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

Why is my array so empty?

Discussion in 'XenForo Development Discussions' started by gordy, Feb 26, 2012.

  1. gordy

    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 $controllerXenForo_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>
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
    gordy likes this.
  3. gordy

    gordy Well-Known Member

    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 $controllerXenForo_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="$vlistsvalue="$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...
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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:

    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:

    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:

    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.
     
    gordy likes this.
  5. gordy

    gordy Well-Known Member

    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 $controllerXenForo_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!
     

Share This Page