Hello,
I’m fairly new to modx (about a week of development). I’ve been through the wiki a few times, and I’m looking to be able to generate random quotes in a template.
Any one have any ideas? I’ve found a few tutorials that use a TV to allow selection of specific chunks to be displayed, I want to have say 5 categories (that can be selected) and then quotes from each category randomly display on selected (topic) pages.
Even a link to a tutorial would be kick-ass.
cheers.
Hi there, I’m no expert but here is something that may help you on your way;
I had to create a snippet that grabbed all the chunks from a particular category and this is what I used:
<?php
//forgetting this could case headaches
global $modx;
//handy variables for the sql call
//get the following fields
$fields= (isset($fields))? $fields :"id,name,description,category,snippet";
//from this table
$tbl = (isset($tbl))? $tbl :$modx->getFullTableName("site_htmlsnippets");
//get the id of the category we are calling from the categories table
$catTbl = $modx->getFullTableName("categories");
$catField = "id";
$catDescription = (isset($catDescription))? $catDescription : 'Default_name';
$sql = 'SELECT '.$catField.' FROM '.$catTbl.' WHERE category = \''.$catDescription.'\';';
$result = $modx->dbQuery($sql);
$catArray = array();
for($i=0;$i<@$modx->recordCount($result);$i++) {
array_push($catArray,@$modx->fetchRow($result));
}
$catNo=$catArray[0]['id'];
//this is just in case it can't find the category name it tells us and uses category id of 1
if(!isset($catNo)){
echo "The category: <b>".$catDescription."</b> could not be found. Using a category ID of 1 (Check your category table)";
$catNo=1;
}
//get our fields from the db and save them into a multidimensional array
$sql = 'SELECT '.$fields.' FROM '.$tbl.' WHERE category = '.$catNo.';';
$result = $modx->dbQuery($sql);
$resourceArray = array();
for($i=0;$i<@$modx->recordCount($result);$i++) {
array_push($resourceArray,@$modx->fetchRow($result));
}
//we have the array...so lets use $rand_key to grab a random entry
$rand_key = array_rand($resourceArray);
//and output it
echo "this is for ".$rand_key." <br />".$resourceArray[$rand_key]['snippet'];
?>
I do hope it works...
All the best,
Taff
Adrian Lawley: www.adrianlawley.com