You are here:  » Random sort results


Random sort results

Submitted by filser on Thu, 2010-05-20 18:03 in

Hi.
Is it possible to randomly order the items of an XML feed so that each time the XML-feed is fetched the result is differently sorted.
Thanks.

Submitted by support on Thu, 2010-05-20 18:13

Hi,

What you need to do is instead of displaying each record in myRecordHandler(), add the record to a global array ($records), and then after the parse shuffle the $records array, and display with your original myRecordHandler using a foreach loop. For example:

<?php
  
function myDisplayRecordHandler($record)
  {
    
// exactly as your original myRecordHandler
  
}
  function 
myRecordHandler($record)
  {
    global 
$records;
    
$records[] = $record;
  }
  
$records = array();
  
MagicParser_parse("filename.xml","myRecordHandler","xml|FORMAT/STRING/");
  
shuffle($records);
  foreach(
$records as $record)
  {
    
myDisplayRecordHandler($record):
  }
?>

Hope this helps!
Cheers,
David.

Submitted by filser on Thu, 2010-05-20 18:41

Great ! Works like a charm ! Thanks a lot.