Need fresh eyes

Cupara

Well-known member
Ok I have multiple forum chooser for my Latest Threads block. The issue is once I have it pull the option it still only displays 1 result when it should display 5.

Here is my code:
PHP:
        $ltOption = XenForo_Application::get('options')->xportal_latestThreads;
$ltMatches = implode(',', $ltOption);

        $lThreads = $this->limitQueryResults('
            SELECT *
            FROM xf_thread
            WHERE node_id = ?
            AND discussion_state = "visible"
            AND discussion_open = "1"
            ORDER BY thread_id DESC
        ', 5);

        return $this->_getDb()->fetchAll($lThreads, $ltMatches);
I have done a print_r on $ltMatches, it is displaying like it should so I know its not in the option.

So any help would be much appreciated.

Thanks
 
Ah... simple mistake Steve...
Code:
WHERE node_id = ?
You are using the 5 as the node_id in your prepared statement, Therefore its defaulting the limit to 1. That being said, why are you writing your own query instead of using the built in thread fetching functions?

This code below is the code I use for my portal. It gets threads not deleted or moderated. It also fetches forum and user information. It also detects whether the user has read/watched/posted on a thread. Then it sorts. Finally it prepares the thread for viewing based on permissions.
Code:
        $visitor = XenForo_Visitor::getInstance();

        $conditions = array(
            'deleted' => false,
            'moderated' => false
        );

        $fetchOptions = array(
            'join' => XenForo_Model_Thread::FETCH_FORUM | XenForo_Model_Thread::FETCH_USER,
            'permissionCombinationId' => $visitor['permission_combination_id'],
            'readUserId' => $visitor['user_id'],
            'watchUserId' => $visitor['user_id'],
            'postCountUserId' => $visitor['user_id'],
            'order' => 'last_post_date',
            'orderDirection' => 'desc',
            'limit' => 5,
        );

        $threads = $this->getModelFromCache('XenForo_Model_Thread')->getThreads($conditions, $fetchOptions);

        foreach ($threads AS $threadID => &$thread)
        {
            if ($this->getModelFromCache('XenForo_Model_Thread')->canViewThreadAndContainer($thread, $thread))
            {
                $thread = $this->getModelFromCache('XenForo_Model_Thread')->prepareThread($thread, $thread);
            }
            else
            {
                unset($threads[$threadID]);
            }
        }

        return $threads;
Actually, its not exactly what I use. In my portal, I wrote my own "getThreads" function in order to handle node_ids.
 
Wow, thanks. I use the 5 in the same manner when pulling from 1 node_id so I assume when using a node_id in an array the limit isn't read properly.
 
Jaxel, thanks again for the code, works like a charm. Is there a way to use a WHERE clause to only pull the threads from forums specified in an option?
 
threadmodel includes already a method for this!
start reading the code:P
Code:
/**
* Gets threads that belong to the specified forum.
*
* @param integer $forumId
* @param array $conditions Conditions to apply to the fetching
* @param array $fetchOptions Collection of options that relate to fetching
*
* @return array Format: [thread id] => info
*/
public function getThreadsInForum($forumId, array $conditions = array(), array $fetchOptions = array())
 
Can't read the code if I don't know the file it came from. :P

I missed the condition array at the top. Oops.
 
Little hint:

99% of the read operations happens in the models (xenforo/model )
YOu want to read threads, so open xenforo/model/thread
As next, check all available methods (because you want to get something, start searching for getXXXXX
blub1234.webp

as you see, it isn't hard and much faster as starting an thread and hoping somebody replies^^
 
Thanks for the tip.

I need a little assistance as I'm having issues getting this to work properly:
PHP:
    public function getThreads()
    {
 $visitor = XenForo_Visitor::getInstance();
 $ltOption = XenForo_Application::get('options')->xportal_latestThreads;
$ltMatches = explode(',', $ltOption);

        $conditions = array(
            'deleted' => false,
            'moderated' => false,
       'node_id' => $ltMatches
        );

        $fetchOptions = array(
            'join' => XenForo_Model_Thread::FETCH_FORUM | XenForo_Model_Thread::FETCH_USER,
            'permissionCombinationId' => $visitor['permission_combination_id'],
            'readUserId' => $visitor['user_id'],
            'watchUserId' => $visitor['user_id'],
            'postCountUserId' => $visitor['user_id'],
            'order' => 'last_post_date',
            'orderDirection' => 'desc',
            'limit' => 5,
        );

        $threads = $this->getModelFromCache('XenForo_Model_Thread')->getThreads($conditions, $fetchOptions);

        foreach ($threads AS $threadID => &$thread)
        {
            if ($this->getModelFromCache('XenForo_Model_Thread')->canViewThreadAndContainer($thread, $thread))
            {
                $thread = $this->getModelFromCache('XenForo_Model_Thread')->prepareThread($thread, $thread);
            }
            else
            {
                unset($threads[$threadID]);
            }
        }

        return $threads;
    }

I'm not even sure how a foreach should be constructed in the template since one exists in the php file. Also the condition for node_id, am I doing something wrong?
 
$ltMatches = explode(',', $ltOption);

$conditions = array(
'deleted' => false,
'moderated' => false,
'node_id' => $ltMatches
);


that's not possible!

you can only pass 1 node_id
 
Top Bottom