I have a script that grabs an xml of my database generated by php and I would like to shuffle the rows before the php script echos the xml so that each time i access the database xml file, I’ll receive the database in a different order.
here’s part of my php script that outputs the xml:
$dom = new DOMDocument("1.0");
$node = $dom->createElement("database");
$parnode = $dom->appendChild($node);
$query = "SELECT * FROM database WHERE 1";
$result = mysql_query($query);
header("Content-type: text/xml");
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = dom->createElement("data");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("id", $row['id']);
$newnode->setAttribute("name", $row['name']);
$newnode->setAttribute("date", $row['date']);
$newnode->setAttribute("latitude", $row['latitude']);
$newnode->setattribute("longitude", $row['longitude']);
}
This is where I’d like to randomize the xml output if possible. This seems like the most logical place, but if there’s a better place, that’s fine with me.
echo $dom->saveXML();
Here’s a sample of my xml:
<database>
<data id="1" name="blah" date="2012-10-10" latitude="0" longitude="0"/>
<data id="3" name="blah" date="2012-10-10" latitude="0" longitude="0"/>
<data id="4" name="blah" date="2012-10-10" latitude="0" longitude="0"/>
</database>
Simply put, I would like the xml rows to be in a different order each time i access it. Thanks for your help.
One way around this problem is to use a temporary array to store the rows fetched from DB, then shuffle this array, then walk through it:
The other way is use the original array as it is, but randomize the insertion process instead: