I'm using PHP to output data from an XML file. It's defaulting to chronological order, which I'd like to reverse. Is this possible with PHP? If so, how?
Here's my page and code:
$file = 'http://www.gostanford.com/data/xml/events/m-baskbl/2010/index.xml';
$xml = simplexml_load_file($file);
foreach ($xml as $event_date) {
if(!empty($event_date->event['vn']) && !empty($event_date->event['hn']) && !empty($event_date->event['vs']) && !empty($event_date->event['hs']))
{
echo '<li>';
echo '<h3>', $event_date->event['vn'], ' vs ', $event_date->event['hn'], '</h3>';
echo '<p><strong>', $event_date->event['vs'], ' - ', $event_date->event['hs'], '</strong></p>';
echo '<p>', date('F j, Y', strtotime($event_date['date'])), '</p>';
echo '</li>';
}
}
Thanks for your help!
As the first answer notes, this may not be a good idea on big XML documents. The simplest and most portable code for PHP 5.1.2 and above may be to use SimpleXML. It may have been built in to earlier PHP versions, but it is standard after 5.1.2.
<?php
$file = 'http://www.gostanford.com/data/xml/events/m-baskbl/2010/index.xml';
$xml = simplexml_load_file($file);
if ($xml === false) {
echo "Couldn't load file\n";
exit (1);
}
$xmlArray = array();
foreach ($xml as $event_date) $xmlArray[] = $event_date;
$xmlArray = array_reverse($xmlArray);
foreach ($xmlArray as $event_date) {
if(!empty($event_date->event['vn']) && !empty($event_date->event['hn']) && !empty($event_date->event['vs']) && !empty($event_date->event['hs']))
{
echo '<li>';
echo '<h3>', $event_date->event['vn'], ' vs ', $event_date->event['hn'], '</h3>';
echo '<p><strong>', $event_date->event['vs'], ' - ', $event_date->event['hs'], '</strong></p>';
echo '<p>', $event_date['date'], '</p>';
echo '<p>', $event_date->event['local_time'], '</p>';
echo '</li>';
}
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With