Undefined index: user_id

Quiver

Active member
Could someone please explain this?

Code:
<?php
class XenTrCom_TopPoster_Model_XTopPoster extends Xenforo_Model_User
{
        public static function XenTrCom_TopPosterArray() {
                $db = XenForo_Application::get('db');
                $userModel = new Xenforo_Model_User;
                $XenTrCom_TopPoster = array();
                $options = XenForo_Application::get('options');
                $numShown = XenForo_Application::get('options')->xentrcom_tp_show;
                $long = "3600";
 
    if (XenForo_Application::get('options')->xentrcom_tp_enable)
    {
    $excludedusergroup = '';
    if (XenForo_Application::get('options')->xentrcom_tp_tpuyegrubu)
    {
      $excludedusergroup = XenForo_Application::get('options')->xentrcom_tp_tpuyegrubu;
      $excludedusergroup = "AND user_group_id NOT IN ($excludedusergroup)";
    }
$TTArray = $db->fetchAll( $db->limit( "SELECT u.username AS username, COUNT( * ) AS totalPosts FROM xf_post AS p LEFT JOIN xf_user AS u ON ( u.user_id = p.user_id ) WHERE post_date > UNIX_TIMESTAMP()-$long GROUP BY p.user_id ORDER BY totalPosts DESC", $numShown ));
                if(sizeof($TTArray) != 0) {
 
                foreach($TTArray as $TTX) {
                        $TTIds[] = $TTX['user_id'];
                }
                $userObjs = $userModel->getUsersByIds($TTIds,array());
                foreach($TTArray as $TT) {
                  if ($TT['user_id'])
      {
      $hrefx = XenForo_Link::buildPublicLink('toposters', $TT);
 
                  }
$XenTrCom_TopPoster[] = array("user" => $userObjs[$TT['user_id']], "username" => $TT['username'], "totalPosts" => $TT['totalPosts']);
 
                }
                }
 
                if(count($XenTrCom_TopPoster))
    {
    return $XenTrCom_TopPoster;
    }
                }
 
        }
}

And why it's displaying this:

Server Error

Undefined index: user_id
  1. XenForo_Application::handlePhpError() in XenTrCom/TopPoster/Model/XTopPoster.php at line 24
  2. XenTrCom_TopPoster_Model_XTopPoster::XenTrCom_TopPosterArray() in XenTrCom/TopPoster/Controller/Public.php at line 10
  3. XenTrCom_TopPoster_Controller_Public->actionIndex() in Arcade/Extend/ControllerPublic/Index.php at line 23
  4. Arcade_Extend_ControllerPublic_Index->actionIndex() in XenForo/FrontController.php at line 310
  5. XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 132
  6. XenForo_FrontController->run() in /home/<myusername>/public_html/index.php at line 13
 
In your query, "SELECT u.username AS username, COUNT( * ) AS totalPosts FROM xf_post" you're not actually grabbing the user_id... Other random stuff I noticed is not sure why you're extending the user model, and you're not using the right casing on "XenForo_Model_User". It probably would be a better idea to call the factory method XenForo_Model::create('XenForo_Model_User'); You've also set an options variable but you're still using the full XenForo_Application::get('options') each time.
 
In your query, "SELECT u.username AS username, COUNT( * ) AS totalPosts FROM xf_post" you're not actually grabbing the user_id... Other random stuff I noticed is not sure why you're extending the user model, and you're not using the right casing on "XenForo_Model_User". It probably would be a better idea to call the factory method XenForo_Model::create('XenForo_Model_User'); You've also set an options variable but you're still using the full XenForo_Application::get('options') each time.
Oh dear. Anyone know what I need to do to fix this?
Because I almost had it working earlier...almost. :-/
 
The query is only selecting u.username and count(*)

It needs to look something like this (take extra note of the indentation used. It's only an aesthetical thing but it makes your code easier to read):

PHP:
		$TTArray = $db->fetchAll($db->limit("
			SELECT u.username AS username, u.user_id,
			COUNT(*) AS totalPosts
			FROM xf_post AS p
			LEFT JOIN xf_user AS u ON
			(u.user_id = p.user_id) 
			WHERE post_date > UNIX_TIMESTAMP()-$long
			GROUP BY p.user_id
			ORDER BY totalPosts DESC
		", $numShown ));

And take note of the points that Despair made.

Any XenForo model should be called like this:

PHP:
$userModel = XenForo_Model::create('XenForo_Model_User');

Always review existing XenForo code. You won't ever see a model being called using "new XenForo_Model_User"
 
The query is only selecting u.username and count(*)

It needs to look something like this (take extra note of the indentation used. It's only an aesthetical thing but it makes your code easier to read):

PHP:
 $TTArray = $db->fetchAll($db->limit("
SELECT u.username AS username, u.user_id,
COUNT(*) AS totalPosts
FROM xf_post AS p
LEFT JOIN xf_user AS u ON
(u.user_id = p.user_id)
WHERE post_date > UNIX_TIMESTAMP()-$long
GROUP BY p.user_id
ORDER BY totalPosts DESC
", $numShown ));

And take note of the points that Despair made.

Any XenForo model should be called like this:

PHP:
$userModel = XenForo_Model::create('XenForo_Model_User');

Always review existing XenForo code. You won't ever see a model being called using "new XenForo_Model_User"
Thanks, and how would I fetch the user's homepage from this as well, without messing up the query?
FYI, the query is supposed to be selecting the top poster of the last hour (not the top poster overall) ;)
I'd like to get their homepage details as I'm looking to display the top poster's homepage. Thanks.
 
Update: I appear to have got it working by LEFT JOINing the xf_user_profile and grabbing homepage from there. Check http://www.twistedpromotion.com/forum/ it should display the top poster of the last hour in the sidebar, along with their homepage (should they have set one). If there hasn't been any posters in the last hour it won't show anything, which makes it better than showing no top poster. LOL! After hours of database errors, I'd call this a victory. Thanks for the help guys. You especially Chris, as your fix solved my problem. :)
 
Top Bottom