• 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

Jake Bunce

XenForo moderator
Staff member
#1
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>
 

Chris D

XenForo developer
Staff member
#4
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>
I personally think you should put this in the RM, Jake.

Very useful. Thank you.