Page php callback. My first Xenforo coding. Help!

This is baffling. So how would I return some HTML to be displayed (as per the code in my first post), and then display it? Or is that really not the best way to approach it?

[edit]
Ok I created a file called ScanTokens.php and and uploaded it to library/AVForums/ScanTokens/ControllerPublic
I set up the class and function to be AVForums_ScanTokens_ControllerPublic_ScanTokens and getScanToken.

Here is the php script.
PHP:
<?php
class AVForums_ScanTokens_ControllerPublic_ScanTokens
{
    public static function getScanToken(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    {
        $visitor = XenForo_Visitor::getInstance()->toArray();
        if (!$visitor['user_id']) {
           
            print_r($visitor);
           

        }
   
    }
}
?>
I don't know what the value of $visitor will be, and I want the user groups, so I tried to print_r it, but nothing happens. Can anyone help please?
Thanks
 
Last edited:
If you're logged in, try if ($visitor['user_id']) instead of if (!$visitor['user_id']). Currently you're only applying the print_r() line if a guest visits the page.
 
Thanks. So far, so good. Now I'm filling a variable called $html with the stuff I want to display in the page.
I tried
Code:
return $html
and in the page
Code:
{$html}
but that didn't display the html.
How do I display the html I have built up?
Thanks.
 
I never made a XenForo page with a php callback. Try
PHP:
 echo $html;
, does it output in the content area or above the rest of the template?
 
It displays exactly the correct HTML immediately after the body tag.
I'm imagining I can return a variable and include that in the HTML of the page somehow.
 
Not coming through.
[edit]
However, $response->params['html'] = $html; does work, although the html is being displayed as text rather than processed by the browser.
 
Hit a problem.

$db = XenForo_Application::get('db');
$sql = "SELECT token FROM scan_tokens WHERE userid = " . $visitor['user_id'] . " LIMIT 1";
$query_results = $db->fetchRow($sql);
if (count($query_results) == 1) {

Now the query returns zero rows (as expected), but

if (count($query_results) == 1) is true. WTF ?!!!
 
Yeah and doing if ($query_results) works fine, but that if (count($query_results) == 1) is true is really messed up.
Thanks Daniel?
 
Yeah and doing if ($query_results) works fine, but that if (count($query_results) == 1) is true is really messed up.
Thanks Daniel?
http://www.copterlabs.com/blog/strict-vs-loose-comparisons-in-php/

If you use (count($query_results) !== 0), it should work (I haven't checked). But as Daniel said, you don't need that. Use empty() if you really want to use a function.

And even with strict comparisons, you can spend quite some time with them to understand some of their behaviours for someone who never studied programmation. Try to use strpos for example (I've really hated that function for once). If you start to have problems with a conditional, it's better to refer to this page.
 
Yeah and doing if ($query_results) works fine, but that if (count($query_results) == 1) is true is really messed up.
Thanks Daniel?
Sort of. PHP has some weird.... we'll call them easter eggs....

Here's an example from php.net of count.

PHP:
<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
// $result == 3

$b[0] = 7;
$b[5] = 9;
$b[10] = 11;
$result = count($b);
// $result == 3

$result = count(null);
// $result == 0

$result = count(false);
// $result == 1
?>

When it returns the query result, if it has no rows it returns false. Since you're only fetching one row regardless, you expect to get 1 result. So count really cannot be used in this situation.. If anything you want to check either if ($query_results !== false) or if (!empty($query_results)) or if ($query_results)
 
Top Bottom