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

Performing a query on a page node

Discussion in 'XenForo Development Discussions' started by Lee, Apr 3, 2013.

  1. Lee

    Lee Well-Known Member

    Is there a way to query the database via a page node?

    Baby steps, please. :LOL:
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  3. Lee

    Lee Well-Known Member

    Just wanted to say a belated thanks for the reply Jake, helped out loads.

    I have a quick question though, if I want to query the same database as my forum installation do I still need to supply the database details again, or does xenforo just "know"?
  4. Lee

    Lee Well-Known Member

    I have this now;

    class Callback_PageNode
        public static function myfunction(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    $db = XenForo_Application::get('db');
            $results = $db->query('
                    SELECT *
                    FROM `xf_thread`
                    WHERE article = 1
            $response->params['results'] = $results;
    I have edited the table xf_thread to allow for the article field and I am sure the query is running, but how to do I output the data on my page? I'm not even sure that I have saved this in an array (do I need to?).
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Accessing the db object (like in your previous post) will use the same database as your XF forum without having to specify the MySQL credentials.

    A code example is in my post here:

    Lee likes this.
  6. Lee

    Lee Well-Known Member

    So taking your code from that example I now have

    <xen:foreach loop="$results" value="$results" i="$i" count="$count">
        {$results.title}, {$results.username}

    EDIT: because that doesn't output anything on my page.
  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member



    The value needs to be different than the loop. It's a foreach. Then reference the value inside of the foreach ($result instead of $results).
  8. Lee

    Lee Well-Known Member

    Like this?
    <xen:foreach loop="$results" value="$result" i="$i" count="$count">
        {$result.title}, {$result.username}
    Then I can use {$result.title}, etc wherever in my page?

    Doesn't seem to output anything still. I wonder if my query if working right....
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  10. Lee

    Lee Well-Known Member

    Thanks, Jake. That seemed to work.
  11. Lee

    Lee Well-Known Member

    Next questions, if I want to then find the first post of the threads associated with the threads found with that query, what would be the best way?

    I am assuming I need to use some sort of join and query pased on the post.thread_id matching the thread id. Correct? Any examples I can work from? Just trying to learn the basics here :)

Share This Page