Pull forums from select category

LPH

Well-known member
How do I get a listing of only forums from a select category? Is there something that can be added to the $node_qry to return a narrowed list?

This code will return a listing of all forums (limited to whatever is set) - and works well.

PHP:
        $number_of_forums = $instance['numberforums'];

        /** Script to pull the forums from XenForo */

        $node_qry = '
                    SELECT node_id, title, node_name, display_in_list FROM `xf_node`
                    WHERE display_in_list = 1 AND node_type_id = "Forum"
                    ORDER BY `node_id` DESC
                    LIMIT ' . $number_of_forums . '
                    ';

        $nodes = XenForo_Application::get( 'db' )->fetchAll( $node_qry );

        $link = '';

        foreach ( $nodes AS $node ) {
                echo(
                    "<div class='entry-meta'><a href='"
                    . $XF->createLink( $link )
                    . XenForo_Link::buildPublicLink( 'forums', $node )
                    . "'>"
                    . XenForo_Helper_String::wholeWordTrim( $node['title'], 40 )
                    . "</a><br />"
                );
        }
 
The database shows parent_node_id is a column. If I force a WHERE statement to parent_node_id = 8 then the code works. But I'm stuck now on getting a variable recognized in the SQL. Ugh.

PHP:
$parent_node_id= $instance['parent_node_id'];
$node_qry = '
                    SELECT node_id, title, display_in_list, parent_node_id FROM `xf_node`
                    WHERE display_in_list = 1 AND node_type_id = "Forum" AND parent_node_id = $parent_node_id
                    ORDER BY `node_id` DESC
                    LIMIT ' . $number_of_forums . '
                    ';

        $nodes = XenForo_Application::get( 'db' )->fetchAll( $node_qry );

This errors out, of course, because of the $parent_node_id in the $node_qry. It is defined and an echo shows it is being returned. The error is a MySQL prepare error.
 
Try:

PHP:
$parent_node_id= $instance['parent_node_id'];
$node_qry = '
SELECT node_id, title, display_in_list, parent_node_id FROM `xf_node`
WHERE display_in_list = 1 AND node_type_id = "Forum" AND parent_node_id =' . $parent_node_id .
'ORDER BY `node_id` DESC
LIMIT ' . $number_of_forums;

$nodes = XenForo_Application::get( 'db' )->fetchAll( $node_qry );
 
Yes. I should have tried escaping. I thought it was one of my many attempts. :p

This works wonders. Thank you !

PHP:
$node_qry = '
                    SELECT node_id, title, display_in_list, parent_node_id FROM `xf_node`
                    WHERE display_in_list = 1 AND node_type_id = "Forum" AND parent_node_id = ' . $parent_node_id . '
                    ORDER BY `title` ASC
                    LIMIT ' . $number_of_forums . '
                    ';

        $nodes = XenForo_Application::get( 'db' )->fetchAll( $node_qry );
 
Top Bottom