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

Please help clean up working code to pull threads based on forum id

Discussion in 'XenForo Development Discussions' started by LPH, Nov 24, 2014.

  1. LPH

    LPH Well-Known Member

    After almost a week, I have working code to pull a forum id from a select that is used in a query to list threads for that forum.

    The code works but is wild that I must wrap quotes around $forum_number at one part to get the query to run properly.

    PHP:
            $forum_number =  $instance['show_forum_number'];

            
    $forum_number " \"$forum_number\" ";

            
    $select_thread_qry '
                        SELECT thread_id, title, node_id, discussion_state FROM `xf_thread`
                        WHERE discussion_state = 1 AND node_id = ' 
    .  $forum_number  '
                        ORDER BY `thread_id` DESC
                        LIMIT 50
                        '
    ;

            
    $select_threads XenForo_Application::get'db' )->fetchAll$select_thread_qry );
    Do you have any suggestions?
     
  2. Insy

    Insy Well-Known Member

    PHP:
    $db XenForo_Application::get('db');

    $forum_number $instance['show_forum_number'];

    $select_threads $db->fetchAll('
       SELECT thread_id, title, node_id, discussion_state
       FROM `xf_thread`
       WHERE discussion_state = 1 AND node_id = ' 
    $db->quote($forum_number) . '
       ORDER BY `thread_id` DESC
       LIMIT 50
    '
    ;
    I've not tested this code
     
  3. LPH

    LPH Well-Known Member

    Ah. I learned something new - PDO::quote !

    Yes. Your code almost works. It is missing the closing parenthesis in the fetchAll but ... once that is put in then it runs the query properly.
     
  4. Daniel Hood

    Daniel Hood Well-Known Member

    It's encouraged to use the XenForo Models plus then you can do it all in one line ;)

    PHP:
    $select_threads XenForo_Model::create('XenForo_Model_Thread')->getThreads(array('node_id' => $instance['show_forum_number'], 'discussion_state' => 1), array('limit' => 50));
     
    LPH likes this.
  5. LPH

    LPH Well-Known Member

    Thank you ! This is exactly what I must learn !

    Version 3 of the bridge is focused on using more of the XenForo code and methods but it's a huge step moving from procedural and building the queries to learning the XF way. But it is exciting to learn better ways to integrate WP and XF.

    (y)
     
  6. LPH

    LPH Well-Known Member

    Wow ! Two lines replaced all of that wild stuff. Best of all, the threads are returned much faster (no actual benchmarks ) !

    (y)

    PHP:
            $feature_node_qry '
                        SELECT node_id, title, display_in_list, node_type_id FROM `xf_node`
                        WHERE display_in_list = 1 AND node_type_id = "Forum"
                        ORDER BY `title` ASC
                        '
    ;

            
    $feature_nodes XenForo_Application::get'db' )->fetchAll$feature_node_qry );

            
    $db XenForo_Application::get('db');

            
    $forum_number $instance['show_forum_number'];

            
    $select_threads $db->fetchAll('
                   SELECT thread_id, title, node_id, discussion_state
                   FROM `xf_thread`
                   WHERE discussion_state = 1 AND node_id = ' 
    $db->quote($forum_number) . '
                   ORDER BY `thread_id` DESC
                   LIMIT 50
                '
    );
    Replaced by

    PHP:

    $feature_nodes 
    XenForo_Model::create('XenForo_Model_Forum')->getForums(array('display_in_list' => ) );

    $select_threads XenForo_Model::create('XenForo_Model_Thread')->getThreads(array('node_id' => $instance['show_forum_number']   , 'discussion_state' => 1), array('limit' => 50));
     
    Last edited: Dec 2, 2014
  7. Nobita.Kun

    Nobita.Kun Well-Known Member

    `discussion_state` should be in range values: 'visible', 'moderated', 'deleted' not `1`
     
  8. LPH

    LPH Well-Known Member

    I see in the database the 'visible' etc but the '1' was working too. Is there are difference?
     
  9. Bob

    Bob Well-Known Member

    AndyB likes this.
  10. LPH

    LPH Well-Known Member

    Thank you Bob. Never do an ORDER BY using the index for enumeration sorting or there might be unexpected results. Got it.

    I also see that the $fetchOptions were wrong for orderDirection. Here is the corrected code.

    PHP:
    $select_threads XenForo_Model::create('XenForo_Model_Thread')->getThreads(array('node_id' => $instance['show_forum_number'], 'discussion_state' => 'visible'), array('limit' => 50'order' => 'thread_id''orderDirection' => 'desc') );
    This seems to be working in the widget.

    One line down ... thousands to go :D
     

Share This Page