1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Undefined index: user_id

Discussion in 'XenForo Development Discussions' started by Quiver, May 13, 2013.

  1. Quiver

    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:

     
  2. luutruong

    luutruong Active Member

    Try
    PHP:
    $userObjs[$TT['user_id']]
    change to
    PHP:
    $userObjs[$TT]['user_id']
     
  3. Despair

    Despair Active Member

    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.
     
  4. Quiver

    Quiver Active Member

    Oh dear. Anyone know what I need to do to fix this?
    Because I almost had it working earlier...almost. :-/
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    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"
     
    KiF and bousaid like this.
  6. Quiver

    Quiver Active Member

    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.
     
  7. Quiver

    Quiver Active Member

    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. :)
     

Share This Page