tags:

views:

909

answers:

2

Hi,

I have started to use the simplexml function that seems to work out better than the previous other parser that I have tried to use. I have got to the stage where I need to the sort the items by transmissiondate - I tried using the uasort but does not make any changes the order of the items.

also some times a programme is on more than once on the same day - would it be easier to sortby videoID can any help?

this what the object looks like:

[0] => SimpleXMLElement Object
    (
        [VideoID] => 108059
        [Genre] => Music
        [ProgrammeName] => MTV
        [OriginalAiringDate] => 2009-11-10T19:22:24
        [TransmissionDate] => 2009-11-10T19:22:24
   )
[1] => SimpleXMLElement Object
    (
        [VideoID] => 108395
        [ExpiryDate] => 2009-12-12T23:59:59
        [DateCreated] => 2009-11-12T13:28:54
        [Genre] => Music
        [ProgrammeName] => MTV
        [OriginalAiringDate] => 2009-11-12T19:22:32
        [TransmissionDate] => 2009-11-12T19:22:32
 )

$xml = simplexml_load_file("data.xml");
$count = 0;
$sortItem = 0;
$dateformat = "D j M, g:ia";
$sortArray = array();
foreach($xml->CatchUp as $item){
$sortArray[$count][TransmissionDate] = $item;
if($count < 4){
print "<p>Programme Name:<strong> "  . $item->ProgrammeName. "</strong></p>";
print "<p>Date Shown:<strong> "  . date($dateformat, strtotime($item->TransmissionDate)). "</strong></p>";
print "<p>Description:<strong> " . trunc($item->ShortSynopsis,30, " ")."</strong></p>";
print "<p><a href='". $item->VideoID. "'>". $item->VideoID."</a></p>";
      $count++;
  }
}

}

asort($sortArray);

A: 

try to use casting when pulling data from your SimpleXMLElement Object

using video id: $sortArray[$count][VideoID] = (int)$item;

cesko80
very new to simpleXML what is casting???
NiseNise
Thanks cedko80, it actually didn't work the order remains the same<code>foreach($xml->CatchUp as $item){ $sortArray[$count]['VideoID'] = (int)$item; if($count < 4){.... $count++; }}asort($sortArray); </code>
NiseNise
+1  A: 

I see two ways of doing that. The first would be to create an array containing the TransmissionDate values, then another array containing the corresponding nodes, then use array_multisort(). This is a bit tedious, so here's what I'd do instead: download SimpleDOM and use sortedXPath()

include 'SimpleDOM.php';

$xml = simpledom_load_file("data.xml");
$dateformat = "D j M, g:ia";

foreach($xml->sortedXPath('CatchUp[ProgrammeName="MTV"]', 'TransmissionDate') as $i => $item)
{
    if ($i == 4)
    {
        // I assume you only want the first 4
        break;
    }
    print "<p>Programme Name:<strong> "  . $item->ProgrammeName. "</strong></p>";
    print "<p>Date Shown:<strong> "  . date($dateformat, strtotime($item->TransmissionDate)). "</strong></p>";
    print "<p>Description:<strong> " . trunc($item->ShortSynopsis,30, " ")."</strong></p>";
    print "<p><a href='". $item->VideoID. "'>". $item->VideoID."</a></p>";
}
Josh Davis
Great Josh it works, one little problem, if I want to filter by name e.g say by only showing the first 4 MTV show only.like if($item->Programme == "MTV"){ } its seems to break
NiseNise
The first argument is an XPath expression. `CatchUp` means "all children nodes named `CatchUp`" - you can add a predicate such as: `CatchUp[ProgrammeName="MTV"]` which transforms it as "all children nodes named `CatchUp` having themselves a child node named `ProgrammeName` whose value is `'MTV'`" - see http://www.w3schools.com/XPath/xpath_syntax.asp
Josh Davis
Talking about XPath, I should also mention that you can use it to limit your search to the first N items by adding the predicate `[position() <= 4]`, removing the need for a counter in PHP. I didn't mention it to keep it simple to beginners, but anyone who has to manipulate XML should take a good look at XPath because it's really powerful and actually quite easy once you learn the basics.
Josh Davis
It seems that every time I filter the data by pulling a specific show the sort mechanism does not work - is there a way in the XPath that I can specify the sort order as it is now displaying the content in reverse order.
NiseNise
Make your data.xml available somewhere and I'll take a look at it. If that's a bug in SimpleDOM, file a bug at Google Code.
Josh Davis