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

Jake Bunce

Well-known member
In response to a PM, I am posting the answer here.

Code example:

Rich (BB 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>
 
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
");
 
In response to a PM, I am posting the answer here.

Code example:

Rich (BB 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>
I personally think you should put this in the RM, Jake.

Very useful. Thank you.
 
Top Bottom