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

guide - query records from a different db and display them in a page node

Discussion in 'XenForo Development Discussions' started by Jake Bunce, Dec 18, 2012.

  1. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    In response to a PM, I am posting the answer here.

    Code example:

    Code:
    <?php
    
    class Callback_PageNode
    {
    	public static function myfunction(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    	{
    		$mydb = new Zend_Db_Adapter_Pdo_Mysql(array(
    			'host'     => 'localhost',
    			'username' => 'dbuser',
    			'password' => 'dbpass',
    			'dbname'   => 'dbname'
    		));
    
    		$myrows = $mydb->fetchAll("
    			SELECT *
    			FROM mb_bans
    		");
    
    		$response->params['myrows'] = $myrows;
    	}
    }
    
    This file would be at:

    library/Callback/PageNode.php

    Then when you edit your page node the callback would be:

    Callback_PageNode::myfunction

    And you can use {$myrows} in your page template. To display multiple rows you can use a foreach in the page template. Example:

    Code:
    <xen:foreach loop="$myrows" value="$myrow" i="$i" count="$count">
    	{$myrow.field1}, {$myrow.field2}, {$myrow.field3}
    </xen:foreach>
    
     
    EQnoble, MattW, cclaerhout and 5 others like this.
  2. Zwirc

    Zwirc Member

    Thanks again Jake!
    +1 ;)
     
  3. digitalpoint

    digitalpoint Well-Known Member

    If the connection is to the same DB server, just a different database, you can also use the normal DB connection and just specify the database in the query...

    Code:
    $myrows = $mydb->fetchAll("
                SELECT *
                FROM dbname.mb_bans
    ");
     
    bousaid, Chris D and Jake Bunce like this.
  4. Chris D

    Chris D XenForo Developer Staff Member

    I personally think you should put this in the RM, Jake.

    Very useful. Thank you.
     

Share This Page