0

I have done a search for this but I haven't had any luck finding exactly what I am looking for. I am using simpleXML to parse a RSS feed I have made and it works great for showing just a single entry. I am trying to modify this method to just pull the most recent updated entry. How would I get it to update or just pull the most recent entry?

This is what I have now that parses the RSS feed and shows just one single entry but, this is where I am stuck as I would just like to show the most recent entry.

This is just a snippet of the most relevant code that parses the feed.

//Set initial output to false
    $tData = false;
for($i = 0; $i < 1; $i++){

    $location = $xml->reports[$i]->location;
    $upperCase = strtoupper($location);
    $report = $xml->reports[$i]->report;
    $timestamp = $xml->reports[$i]->timestamp;
    $updateTime = DATE("g:i A", STRTOTIME($timestamp));

// Set table style
   $tableStyle = "width: 100%; margin:0px auto; background-color:{$bkgColor};";
   $td1Style = "{$tbrdr};{$sbrdr}; text-align:center; font-size: 11px; background-image:url({$imagesDir}headerbgd2.gif); color:{$dtColor};";
   $td2Style = "{$sbrdr}; text-align:center; font-size: 12px; padding: 1px 0px 1px 0px; background-color:{$bkgColor};";
   $td3Style = "{$sbrdr}; {$bbrdr}; text-align:center; background-color:{$bc};";

 // construct data for table display
    $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n";
    $tData .= "<tbody>\n";
    $tData .= "  <tr><td style='{$td1Style}'>LATEST LOCAL STORM REPORT</td></tr>\n";
    $tData .= "  <tr><td style='{$td2Style}'><b>{$report}</b>&nbsp;&nbsp; - &nbsp;&nbsp;<span style='color: rgb(204, 102, 0);'>{$upperCase} - {$updateTime}</span></td></tr>\n";
    $tData .= "  <tr><td style='{$td3Style}'><a href='wxmesqLSR.php' title='Click to view the details'>Click here for details</a></td></tr>\n";
    $tData .= "</tbody>\n";
    $tData .= "</table>\n";
    $tData .=  $afterTable;

 }

EDIT: Sample of the XML file

<?xml version="1.0"?>
<entrys>
  <reports>
    <timestamp>Thu, 11 Jul 2013 23:19:39 -0500</timestamp>
    <name>Mesquite Weather</name>
    <location>Mesquite</location>
    <report>GENERAL</report>
    <description>Official MW test</description>
  </reports>
  <reports>
    <timestamp>Fri, 12 Jul 2013 00:44:39 -0500</timestamp>
    <name>Mesquite Weather</name>
    <location>Sunnyvale</location>
    <report>DOWNED POWER LINES</report>
    <description>Just an official MW test</description>
  </reports>
</entrys>

-Thanks!

Texan78
  • 687
  • 2
  • 16
  • 42
  • Can you provide a sample of the XML that is being processed? We need to know what identifies the "most recent entry" - is it the last one? Currently your problem is that you don't know how many entries you have in your input-XML and only loop once (as defined by `$i < 1`). This may be solved using an XPath, but not without knowing the XML structure. – tfoo Jul 12 '13 at 17:40
  • @tfoo the `$i < 1` defined so it only returns a single entry. Otherwise if there is multiple entry's it would show more than one and I only want to show one entry, being the most recent. I have updated my OP with a sample of the XML structure. – Texan78 Jul 12 '13 at 19:24

2 Answers2

0

You will have to do some manual date comparison to find the correct entry, unless they are already sorted in the xml. So basically your workflow will have to be:

  1. Loop over all reports nodes and read its timestamp
  2. Convert the timestamp to a comparable value (use strtotime or date) and compare them all to find the most recent reports
  3. Pick up the reports identified as the most recent and use your existing functions

If they are sorted by timestamp in the XML, the most recent entry will either be the first or the last. As by your example the first one is not the one you are looking for, I suspect it could be the last one. If this is the case, simply find the amount of reports elements (easiest using XPath count(//reports) (or similar) and then read the reports element with this index.

tfoo
  • 326
  • 2
  • 11
0

The newest entry is the one with the highest timestamp when converted to a Unix timestamp.

In your current document, this not yet the case. So let's create an array of such Unix timestamps:

$timestamps = array_map('strtotime', $xml->xpath('/*/reports/timestamp'));

To be able to sort on these, the reports as well are needed as they should be sorted:

$reports    = $xml->xpath('/*/reports');

Now you've got an array of timestamps:

Array
(
    [0] => 1373602779
    [1] => 1373607879
)

And an array of the reports:

Array
(
    [0] => SimpleXMLElement Object
        (
            [timestamp] => Thu, 11 Jul 2013 23:19:39 -0500
            [name] => Mesquite Weather
            [location] => Mesquite
            [report] => GENERAL
            [description] => Official MW test
        )

    [1] => SimpleXMLElement Object
        (
            [timestamp] => Fri, 12 Jul 2013 00:44:39 -0500
            [name] => Mesquite Weather
            [location] => Sunnyvale
            [report] => DOWNED POWER LINES
            [description] => Just an official MW test
        )

)

To sort one array based on the values of another array has been outlined already in:

The same principle apply to your scenario in simplexml, with the difference that you need to create these arrays first - and that's what I've demonstrated already.

Hope this is helpful for you. Related questions for sorting simplexml are:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • that makes sense and figured it had to be sorted by date/time. One problem with this. Keep in mind that XML was just an example. I really can't make an array since I don't know how many entries would be in the XML file. It could be anywhere from 1-20+. These entries expire 12 hrs from the time they are submitted. The XML is created from a form on one of my pages that people can submit to the XML feed. So I can change it so the timestamp is a Unix timestamp if needs to be. – Texan78 Jul 12 '13 at 21:33
  • An array has no problem to take 1-20+ entries. Or 200. Or 2 000. An array is a list and the length is not limited (only be memory, however more memory is consumed by simplexml in this case so you should not really worry about that). – hakre Jul 12 '13 at 22:23
  • @harke I understand that part. Maybe I am just a little confused. Don't I need to create the array? If so that is what I am saying is I don't know how long to make the array because I don't know how long the XML would be. it could be 2-3 entries or 20+ entries. So I can't create an array when the number of entries are unknown. Does that make sense? – Texan78 Jul 12 '13 at 23:20
  • In PHP you can create an array without knowing the length. That's no problem at all. You don't need to know the number of entries upfront. http://php.net/array – hakre Jul 12 '13 at 23:23
  • would it be easier to have this in ISO 8601 or RFC 2822 when the entry is added to eliminate having to convert it? – Texan78 Jul 13 '13 at 00:53
  • I know how to create array but this doesn't explain how to `DISPLAY` the most recent report. All these links show is how to sort them and the above solution doesn't work and I believe it may be overkill. http://www.mesquiteweather.net/inc-mesq-lsr-alert.php – Texan78 Jul 13 '13 at 01:18
  • that is why I sort the array. So the first entry is the most recent one. – hakre Jul 13 '13 at 07:38
  • None of this is making any sense. I understand it needs to be sorted but I don't know how to apply this once it's sorted not to mention I am not getting an array when I print it out. It returns nothing with the example you've suggested. – Texan78 Jul 13 '13 at 15:22
  • @Texan78: Well, sorry to read it makes no sense to you. I see that I had already problems to educate you about array fundamentals and it didn't work out, so I think I should not try again with doing output and troubleshooting in PHP. I suggest we put the question on hold probably until more information about your *concrete* programming question comes up and we leave these integration grounds. – hakre Jul 13 '13 at 20:10