mardi 4 avril 2017

Limiting the number of XML items to parse in php that match a specific value

The PHP i have right now is only half working and it is a little clunky. I'm looking to display the 3 most recent press releases from an XML feed that match a specific value type. What I have right now is only looking at the first three items, and just echoing the one's that match the value. I'm also pretty sure DOM object is not the best approach here, but had issues getting xparse to work properly. Some help with the logic would be greatly appreciated.

    <?php
 //create new document object
 $dom_object = new DOMDocument();
 //load xml file
 $dom_object->load("http://ift.tt/2o0EOgN");




$cnt=0;
foreach ($dom_object->getElementsByTagName('press_release') as $node) {
    if($cnt == 3 ) {
       break;
     }

    $valueID = $node->getAttribute('id'); 
    $valueType = $node->getAttribute('type'); 

    $headline = $dom_object->getElementsByTagName("headline");
    $headlineContent  = $headline->item(0)->nodeValue;

    $releaseDate = $dom_object->getElementsByTagName("published");
    $valueDate = $releaseDate->item(0)->getAttribute('date'); 


    $cnt++;

?>    

<?php 

    if ($valueType == 5)
    {
         echo "<div class=\"newsListItem\"> <p> $valueDate </p> <h4>$headlineContent</h4><p></p></div>";
    }

} ?>

Aucun commentaire:

Enregistrer un commentaire