I tweaked LoopDBChunk to work with an external database and thought I would share the steps.
In a document I call a snippet entitled EventLister
which contains the following code :
<?php
include("assets/snippets/eventlister/config.php");
$query="SELECT * FROM events WHERE
(year >= $current_year AND month > $current_month)
OR (year >= $current_year AND month = $current_month AND day >= $current_day)
OR (year_end >= $current_year AND month_end = $current_month AND day_end >= $current_day)
OR (year > $current_year)
OR (year_show >= $current_year AND month_show > $current_month)
OR (year_show >= $current_year AND month_show = $current_month AND day_show >= $current_day)
OR (year_show > $current_year)
ORDER BY year, month, day LIMIT 0, $maxnum";
$paramra = array(
'hostvar' =>$dbhost,
'uservar' => $dbuser,
'passvar' => $dbpass,
'databasevar'=> $dbname,
'sql' =>$query,
'chunkName' =>'displayEvents',
'perPage' => '5',
'orderby' => 'id',
'missingValTxt' => '--');
return $modx->runSnippet('loopDBChunk' , $paramra);
?>
The call to loopDBChunk include four additional parameters, which are pulled from a config file :
the database host, user, password and database name.
The snippet "loopDBChunk" was edited near the end to include the lines with my initials :
$ldParam['footer'] = isset($footer) ? $footer : '';
$ldParam['thehost'] = isset($hostvar) ? $hostvar : ''; //MattC
$ldParam['theuser'] = isset($uservar) ? $uservar : ''; //MattC
$ldParam['thepass'] = isset($passvar) ? $passvar : ''; //MattC
$ldParam['thedatabase'] = isset($databasevar) ? $databasevar : ''; //MattC
require_once($ldPath . 'LoopDBChunk.php');
(Take note that the name of the array variable begins with a lower case "L", not an upper case "I". This can cause confusion and a lot of wasted time depending on what font you are using. Trust me - it took two days to figure out why only the most recent added variables weren’t getting passed all the way through!)
Finally "LoopDBChunk.php" was edited to include these initialed lines as well :
class LoopDBChunk {
var $hostvar = ''; //MattC
var $uservar = ''; //MattC
var $passvar = ''; //MattC
var $databasevar = ''; //MattC
var $tableName = '';
<snip>
function LoopDBChunk($paramArray)
{
$this->hostvar = $paramArray['thehost']; //MattC
$this->uservar = $paramArray['theuser']; //MattC
$this->passvar = $paramArray['thepass']; //MattC
$this->databasevar = $paramArray['thedatabase']; //MattC
$this->tableName = $paramArray['table'];
<snip>
$query .= ' ORDER BY ' . $this->orderby;
}
}
mysql_connect($this->hostvar,$this->uservar,$this->passvar) OR DIE ('Unable to connect to database! Please try again later.'); //MattC
mysql_select_db($this->databasevar) or die( "Unable to select database"); //MattC
//$rs = $modx->db->query($query);
$rs = mysql_query($this->sql); //MattC
//$numRows = $modx->db->getRecordCount($rs);
$numRows = mysql_num_rows($rs); //MattC
if($this->perPage > 0)
{
$from = $pageStart - 1;
$query .= ' LIMIT ' . $from . ', ' . $this->perPage;
}
//$rs = $modx->db->query($query);
$rs = mysql_query($this->sql); //MattC
// echo $query . '<br/>';
$this->chunk = $modx->getChunk($this->chunkName);
$i = 0;
//while( $row = $modx->db->getRow($rs) )
while( $row = mysql_fetch_assoc($rs) ) //MattC
{
$output .= $this->getChunkReplaced($row, $i);
$i++;
}
mysql_close(); //MattC
<snip>
There could probably be more/better error trapping, or even more elegant ways to accomplish the same thing. But I think this will work and I hope it helps someone else.
Matt