XF 2.0 Passing an array from .php to a node

hutch2323

New member
Hello again,

I've encountered another problem with XF 2.0. I'm trying to query my database from a node and display the results on that page. I've done some research, but I think I'm getting some of the syntax from earlier versions of XF mixed in and its not working. Currently, I have a PageNode.php file located in the \src\addons\Callback2 directory. This is the code used in PageNode.php:

PHP:
<?php
 
namespace Callback2;

class PageNode
{
    public static function myfunction()
    {
        $db = \XF::db();
 
        $results = $db->fetchAll('
                SELECT *
                FROM `xf_user`
        ');
  
        $response->params['results'] = $results;
    }
}

?>

On the node page, under the Template HTML section, I have the following:

HTML:
<xf:foreach loop="{$results}" value="{$result}">
   {xf:raw $result.content}
</xf:foreach>

Finally, the PHP callback is:

Callback2\PageNode::myfunction


When visiting the page though, no results are displayed. I really have no idea what the issue is here. Thanks for your time!
 
Your function signature doesn't contain any of the arguments which are actually passed to it.

These callback arguments are listed below the callback fields in the page node setup:
Callback arguments:
  1. \XF\Pub\Controller\AbstractController $controller
    The controller instance. From this you can inspect the request, response etc.
  2. \XF\Mvc\Reply\AbstractReply &$reply
    The standard reply from the page controller.

A few other changes are required too. It would have to look something like this instead:

PHP:
<?php

namespace Callback2;

class PageNode
{
    public static function myfunction(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
    {
        $db = \XF::db();

        $results = $db->fetchAll('
                SELECT *
                FROM `xf_user`
        ');

        $reply->setParam('result', $results);
    }
}
 
Template wise, another few changes. You no longer need curly brackets around variables in certain places which expect a variable or advanced conditions/code and we have a new system for template functions and filters which require a slightly different syntax. With that in mind:
HTML:
<xf:foreach loop="$results" value="$result">
   {$result.content|raw}
</xf:foreach>
 
@Chris D Thanks for the help. I tried updating the code to match what you had suggested, but I'm still not seeing any results displayed. Should I have any other HTML code besides those three lines? I checked the database as well to ensure that there were entries in the table and also tried another table. Any further suggestions?
 
This: {$result.content|raw} suggests you have a field in the xf_user table named content. That's not a default field so I'm not sure if that's related.
 
@Chris D Thanks again. I thought that it may be the issue, as content is not a field in the table. I replaced it with user_id (see below), but it still gives me no results

HTML:
<xf:foreach loop="$results" value="$result">
   {$result.user_id|raw}
</xf:foreach>
 
Ah there’s a typo in my code example above. Sorry about that. Try:
PHP:
$reply->setParam('results', $results);
 
@Chris D Perfect! It worked! I do have one more question if you don't mind me asking.

I want to be able to send a specific user_id to the .php file from the node (HTML) and query that user only. I know how to set it up through the .php file, but I'd like to have the ability to do it from my webpage rather than hardcoding. How would I go about setting something like that up?
 
Back
Top Bottom