Problem in using PHP callback

tonnyz

Member
Hi, I am trying to show this php output on my member_view template

PHP:
<?php
class GeekPoint_Last{
   
    public static function getHtml(){
// get latest x forum posts from xenforo by liamdawe

// edit this to whatever folder your forum is in
$forum_url = 'C:/xampp/';

// change to the amount of posts you want displayed
$limit = 7;

// edit this to the directory your forum is in
$forum_directory = 'C:/xampp/htdocs/community/';

$startTime = microtime(true);

require($forum_directory. 'library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($forum_directory . '/library');

GeekPoint_Symfony::initializeXenforo($forum_directory, $startTime);

$nodeModel = XenForo_Model::create('XenForo_Model_Node');
$nodes = $nodeModel->getViewableNodeList();

$nodes_get = array_keys($nodes);

$node_id_list = implode($nodes_get, ',');

$sql_forum = "SELECT `title`, `thread_id`, `view_count`, `reply_count` FROM `xf_thread` WHERE `node_id` IN ($node_id_list) ORDER BY `last_post_date` DESC LIMIT {$limit}";

$topics_query = XenForo_Application::get('db')->fetchAll($sql_forum);



foreach ($topics_query as $thread)
{
    // The absolute thread url is constructed for you
    $threadUrl = XenForo_Link::buildPublicLink('full:threads', $thread);

    // Trimmed and properly escaped
    $threadTitle = XenForo_Template_Helper_Core::helperWordTrim($thread['title'], 50);

    echo "<a href=\"$threadUrl\">$threadTitle</a> Views: {$thread['view_count']}, Comments: {$thread['reply_count']}<br />
<br />";
}

    }
}
?>

I tried to insert the php callback inside my member_view template as so:
Code:
<xen:callback class="GeekPoint_Last" method="getHtml"></xen:callback>

but it just showed a blank page written:
Fatal error: Cannot redeclare class XenForo_Autoloader in C:\xampp\htdocs\community\library\XenForo\Autoloader.php on line 17

Any help please ? thx :)
 
It looks to me like you're re-initializing XenForo but actually, in the context of a callback tag in a template, XenForo is already initialized.

You probably want to leave in the $limit variable, but otherwise, the rest of this is superfluous:
PHP:
// edit this to whatever folder your forum is in
$forum_url = 'C:/xampp/';

// change to the amount of posts you want displayed
$limit = 7;

// edit this to the directory your forum is in
$forum_directory = 'C:/xampp/htdocs/community/';

$startTime = microtime(true);

require($forum_directory. 'library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($forum_directory . '/library');

GeekPoint_Symfony::initializeXenforo($forum_directory, $startTime);
 
It looks to me like you're re-initializing XenForo but actually, in the context of a callback tag in a template, XenForo is already initialized.

You probably want to leave in the $limit variable, but otherwise, the rest of this is superfluous:
PHP:
// edit this to whatever folder your forum is in
$forum_url = 'C:/xampp/';

// change to the amount of posts you want displayed
$limit = 7;

// edit this to the directory your forum is in
$forum_directory = 'C:/xampp/htdocs/community/';

$startTime = microtime(true);

require($forum_directory. 'library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($forum_directory . '/library');

GeekPoint_Symfony::initializeXenforo($forum_directory, $startTime);

Works like magic ! Thanks :LOL:
 
It looks to me like you're re-initializing XenForo but actually, in the context of a callback tag in a template, XenForo is already initialized.

@Chris D , I have 1 more problem :

The code above will count all viewable node list in my forum. How should I exclude the threads under certain (node) category from being counted (without listing the array manually with $nodes_get = array(1,2,3,4); )?

Many thanks :)
 
Last edited:
Top Bottom