Performing a query on a page node

Lee

Well-known member
Is there a way to query the database via a page node?

Baby steps, please. :LOL:
 
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"?
 
I have this now;

Code:
<?php
 
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?).
 
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"?

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.

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

A code example is in my post here:

http://xenforo.com/community/thread...ent-db-and-display-them-in-a-page-node.42311/
 
  • Like
Reactions: Lee
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:

http://xenforo.com/community/thread...ent-db-and-display-them-in-a-page-node.42311/

So taking your code from that example I now have

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

correct?


EDIT: because that doesn't output anything on my page.
 
Use:

value="$result"

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

value="$result"

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

Like this?
Code:
<xen:foreach loop="$results" value="$result" i="$i" count="$count">
    {$result.title}, {$result.username}
</xen:foreach>

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....
 
Change to:

Rich (BB code):
        $results = $db->fetchAll('
                SELECT *
                FROM `xf_thread`
                WHERE article = 1
        ');

This is also in my example:

http://xenforo.com/community/thread...ent-db-and-display-them-in-a-page-node.42311/

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 :)
 
Back
Top Bottom