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

Problem in using PHP callback

Discussion in 'XenForo Development Discussions' started by tonnyz, Dec 15, 2014.

  1. tonnyz

    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:
    Any help please ? thx :)
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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);
     
    tonnyz likes this.
  3. tonnyz

    tonnyz Member

    Works like magic ! Thanks :LOL:
     
  4. tonnyz

    tonnyz Member

    @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: Dec 15, 2014

Share This Page