Why is my array so empty?

gordy

Well-known member
I'm trying to use this as a PHP callback into a page.
The video tutorial is little help for anything not builtin function wise or custom queries.

I've got this :

PHP:
<?php
class PFvbench_vlist
{
    public static function showVlist(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    {
        $vbenchlists = XenForo_Application::get('db')->fetchAll('SELECT *
                FROM xf_user m LEFT JOIN xf_user_field_value p on p.user_id = m.user_id
                WHERE field_value = "vbench"
                ORDER BY username ASC');
 
                foreach ($vbenchlists AS &$vlists)
                {
                        $vlists = array(
                        'userid' => $vlists['user_id'],
                        'vname' => $vlists['username']);
                        print "$vlists";
                return $vbenchlists;
                }
                $response->templateName = 'vbench_list_template';
    }
}

The query by itself against the database works fine showing expected results.

And is this part right getting it into it's own template?

$response->templateName = 'vbench_list_template';

The template (exists)

Code:
<xen:foreach loop="$vlist" value="$vbenchlists">
<li>
<a href="{$username}{$user_id}/#vbench">{$username}</a><br /><br />
</li>
</xen:foreach>
 
Code:
                foreach ($vbenchlists AS &$vlists)
                {
                        $vlists = array(
                        'userid' => $vlists['user_id'],
                        'vname' => $vlists['username']);
                        print "$vlists";
                return $vbenchlists;
                }

I'm not sure why you are using print. And you have a return inside of the loop, so it will return after the first record and never process the other records, and it won't get to the last line that defines your content template. Also note that you are overwriting each record with an array containing only those two values.

You also need to put your new params into the response at some point. $response->params contains the viewParams from the page controller. It's an array of param values. You can change it within your callback function.
 
Thank you, I'm still not getting output after removing the return (the print was just to try and see some output running the php from cli)

So no I have this:
PHP:
<?php
class PFvbench_vlist
{
    public static function showVlist(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    {
        $vbenchlists = XenForo_Application::get('db')->fetchAll('SELECT m.user_id, username, field_value
                FROM xf_user m LEFT JOIN xf_user_field_value p on p.user_id = m.user_id
                WHERE field_value = "vb3nch"
                ORDER BY username ASC');
 
                foreach ($vbenchlists AS &$vlists)
                {
                        $vlists = array(
                        'userid' => $vlists['user_id'],
                        'vname' => $vlists['username']);
                }
                $response->params;
                $response->templateName = 'vbench_list_template';
   }
}

And my template look like this:

PHP:
<xen:foreach loop="$vlists" value="$vlists ">
<li>
<a href="/members/{$username}{$userid}/#vbench">{$username}</a><br /><br />
</li>
</xen:foreach>
 
Hello!

(the hello is a render test)

And my page looks like this:

<xen:include template='vbench_list_template' />

With a callback of:

PFvbench_vlist::showVlist

I can see the Hello! render fine on my pages page, it's just the other data is empty, even with the changes you've mentioned.

I've got to be missing something...
 
You didn't assign anything to the params. For example, do this:

Code:
$response->params['vbenchlists'] = $vbenchlists;

That should then allow your foreach to work in the template. But you need to name the parameter as "loop" in the foreach:

Rich (BB code):
<xen:foreach loop="$vbenchlists" value="$vlists ">
<li>
<a href="/members/{$username}{$userid}/#vbench">{$username}</a><br /><br />
</li>
</xen:foreach>

And you need to actually use the "value" variable or the loop makes no sense:

Rich (BB code):
<xen:foreach loop="$vbenchlists" value="$vlists">
<li>
<a href="/members/{$vlists.vname}{$vlists.userid}/#vbench">{$vlists.vname}</a><br /><br />
</li>
</xen:foreach>

And you should use xen:link syntax for the link:

Rich (BB code):
<xen:foreach loop="$vbenchlists" value="$vlists">
<li>
<a href="{xen:link members, {$vlists}}">{$vlists.vname}</a><br /><br />
</li>
</xen:foreach>

But xen:link relies on the user record having a user_id and username field which are you overwriting in your callback, so you should change that in your callback.
 
Hooray! It finally works, thank you Jake for your immense help. :notworthy:

I have spent the entire weekend struggling with this and now there is success. While Kiers video was informative, it only calls built in functions and classes which was not much use for folks drawing up their own queries.

Here's the bits for posterity for anyone else who runs across needing to call a php file into a page.

The Callback: ./library/PFVbench/vlist.php
PHP:
<?php
class PFvbench_vlist extends XenForo_ControllerPublic_Abstract
{
    public static function showVlist(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    {
        $vbenchlists = XenForo_Application::get('db')->fetchAll('SELECT m.user_id, username, field_value
                FROM xf_user m LEFT JOIN xf_user_field_value p on p.user_id = m.user_id
                WHERE field_value = "vb3nch"
                ORDER BY username ASC');
 
                foreach ($vbenchlists AS &$vlists)
                {
                        $vlists = array(
                        'userid' => $vlists['user_id'],
                        'vname' => $vlists['username']);
 
                $response->params['vbenchlists'] = $vbenchlists;
                $response->templateName = 'vbench_list_template';
 
                }
    }
}

The template, named " vbench_list_template "
Code:
<xen:foreach loop="$vbenchlists" value="$vlists">
<li>
<a href="/members/{$vlists.vname}.{$vlists.userid}/#vbench">{$vlists.vname}</a><br /><br />
</li>
</xen:foreach>

The pages PHP Callback PFvbench_vlist::showVlist

Thanks again Jake!
 
Top Bottom