Beginners question on php simplexml

They have: 5 posts

Joined: Sep 2009

This is a bit of the XML feed I'm working with:

<LocalEvents Version="1.0">
<Description>Local Events posted to WhereCanWeGo.com.</Description>
<Account>12345</Account>
<Postcode>RG87LP</Postcode>
<DateFrom>01/04/09</DateFrom>
<DateTo>30/04/09</DateTo>
<Distance>10</Distance>
<Categories>1111111111111111111</Categories>
<Regulars>0</Regulars>
<Maximum>10</Maximum>
<Extracted>16 September 2009</Extracted>
<AddEventsURL>http://www.wherecanwego.com/events/signin.aspx</AddEventsURL>
<item>
<eventid>261569</eventid>
<title>St. George's Day service in celebration of England</title>
<Venue>
St. Andrew's Church, Albert Road, Caversham Heights
</Venue>
<Postcode>RG4 7PL</Postcode>
<Times>4.30pm</Times>
<Dates>23 Apr 09 to 24 Apr 09</Dates>
<DetailsURL>
http://www.wherecanwego.com/Search/ViewEvent.aspx?e=261569
</DetailsURL>
</item>
<item>
<eventid>229053</eventid>
<title>Tilehurst Horticultural Association - Evening Talk</title>
<Venue>
The Village Hall, Victoria Road, Tilehurst, Reading.
</Venue>
<Postcode>RG31 5AB</Postcode>
<Times>The Talk commences at 7.45pm</Times>
<Dates>30 Apr 09</Dates>
<DetailsURL>
http://www.wherecanwego.com/Search/ViewEvent.aspx?e=229053
</DetailsURL>
</item>

I want to put the list of item's on a web page sorted in (start)date order.
Some Dates attributes include from and to dates.

I have no idea how to go about this - any help gratefully accepted!

jb

They have: 5 posts

Joined: Sep 2009

well first I do this $xml = simplexml_load_file($xmlfeed);

and I suspect I need to insert a new attribute unixtime to sort with?

something like
$xml->item[something]->addAttribute("unixtime", strtotime(preg_replace('/to.*/','',$xml->item[something]["Dates"])));

I dont know how to add that to each item though.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Welcome to the forums, jbudd! Try something like this...

<?php
/* fetchs the XML from a local file */
$data = file_get_contents('data.xml');

/* parse the XML into a PHP object */
$xml = new SimpleXMLElement($data);

/* loop through all the items in the feed */
$items = array();
foreach (
$xml->item as $field => $item)
{
   
/* grab the first date and process it into a timestamp */
   
if (strpos($item->Dates, ' to ') !== false)
    {
       
$date = explode(' to ', $item->Dates, 2);
       
$time = strtotime($date[0]);
    }
   
/* if there is only one date, no need to split it up */
   
else
    {
       
$time = strtotime($item->Dates);
    }
   
   
/* for debugging purposes
    echo 'Original: ' . $item->Dates . "\n";
    echo 'Time: ' . $time . "\n";
    echo 'Formatted: ' . date('r', $time) . "\n";
    */
   
    /* add the item to a new array, with the timestamp as the key,
       so we can sort it later */
   
$items[$time] = $item;
}

sort($items);

/* see what we got */
header('Content-type: text/plain');
var_dump($items);
?>

I'm basically getting the start date, and converting it to a timestamp. Then form a new array of items, with that the timestamps as the keys, and sort.

If you want to run the example, make sure to save your XML data in the above mentioned XML file.

They have: 5 posts

Joined: Sep 2009

Thanks for the reply

I have/had a couple of problems. First was multiple events on the same day so the array ends up with less elements. To fix it I added a record count to the time (which ends 00 for all these dates)

Second, the sort() does something, but it doesnt sort by the time.

Here is my debug code before sort()
Event 299587 Dates 19 Sep 09 Time: 1253314800 Formatted: Sat, 19 Sep 2009 00:00:00 +0100
Event 299590 Dates 10 Oct 09 Time: 1255129201 Formatted: Sat, 10 Oct 2009 00:00:01 +0100
Event 291193 Dates 10 Oct 09 Time: 1255129202 Formatted: Sat, 10 Oct 2009 00:00:02 +0100
Event 291827 Dates 18 Sep 09 to 19 Sep 09 Time: 1253228403 Formatted: Fri, 18 Sep 2009 00:00:03 +0100
Event 295999 Dates 19 Sep 09 to 20 Sep 09 Time: 1253314804 Formatted: Sat, 19 Sep 2009 00:00:04 +0100

and here it is after sort()
Event 295999 Dates 19 Sep 09 to 20 Sep 09 Time 0
Event 291827 Dates 18 Sep 09 to 19 Sep 09 Time 1
Event 291193 Dates 10 Oct 09 Time 2
Event 299590 Dates 10 Oct 09 Time 3
Event 299587 Dates 19 Sep 09 Time 4

They are not coming out in date order!

I tried asort() too. It retains the key, but the sort order is the same
Event 295999 Dates 19 Sep 09 to 20 Sep 09 Time 1253314804
Event 291827 Dates 18 Sep 09 to 19 Sep 09 Time 1253228403
Event 291193 Dates 10 Oct 09 Time 1255129202
Event 299590 Dates 10 Oct 09 Time 1255129201
Event 299587 Dates 19 Sep 09 Time 1253314800

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

pr0gr4mm3r wrote:
Welcome to the forums, jbudd! Try something like this...
<?php
/* fetchs the XML from a local file */
$data = file_get_contents('data.xml');

/* parse the XML into a PHP object */
$xml = new SimpleXMLElement($data);
?>

Just curious, what is the advantage of using the above as opposed to just using:

$xml = simplexml_load_file($xmlfeed);

I'm still trying to get my head around using PHP for XML.

They have: 5 posts

Joined: Sep 2009

Ahah! ksort() works.

I'll make a php programmer yet!

Thanks for the help.
jb

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

DOH! I should have known that there would be multiple events on the same day. Good catch Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.