tags:

views:

150

answers:

2

Setup: Joomla website on LAMP stack

I have a MySQL table containing some records, these are queried by a simple SELECT on the Joomla article, as pasted below. This specific Joomla website has Caching turned on in Joomla's Global Configuration. I need to randomize the order in which I display the resultset, each time the page is loaded.

Regular php/mysql would offer me two approaches for this: 1. use 'order by RAND()' or any of a number of methods to allow a SELECT query to return reasonably random results. 2. once php gets the result from the SELECT into an array, shuffle the array to get a reasonably random order of array items.

However, as this Joomla instance has Caching turned ON in its Global Configuration, either of the above approaches fails. The first time I load the page the order is randomized, however any further reloads do not cause the order to change, as the page is delivered from cache. The instant the Cache is disabled, both approaches (shuffle/order by rand) work perfectly. What am I missing? How do I override the Global Cache for this specific article? A very simple requirement, that is met by both php and mysql reasonably well, is blocked by the Joomla Cache that I cannot turn off.

The php that returns results from the database.

<pre>
$db = JFactory::getDBO();
$select = "SELECT id FROM jos_mytable;"; //order by RAND()
$db->setQuery($select);
echo $db->getQuery(); //Show me the Query!
$rows = $db->loadObjectList();
//shuffle($rows);
foreach($rows as $row) {
    echo "$row->id";
}
A: 

Hi, I guess you are creating a component or what?

You can consider to clean your component from the cache (perhaps conditionally) after every request:

$cache =& JFactory::getCache('your-component-name'); $cache->clean();

Sander Pham
Thanks for this, I am not using a component, but this information is very useful nevertheless.
Shrinivas
A: 

Hey , Please did u find a solution ,and where to put the code : $db = JFactory::getDBO(); $select = "SELECT id FROM jos_mytable;"; //order by RAND() $db->setQuery($select); echo $db->getQuery(); //Show me the Query! $rows = $db->loadObjectList(); //shuffle($rows); foreach($rows as $row) { echo "$row->id"; }

tifak
There isn't a solution yet, however I have linked to a workaround in my earlier comment. The workaround modifies core Joomla methods to stop caching certain articles, identified by an HTML comment in them. I am using DirectPHP to write custop PHP code modules that can be used within joomla pages.
Shrinivas
Thanks for the answer
tifak