2

I apologize if this is trivial question. I did search and didn't find an answer to my question so I am posting. Thanks upfront for whoever can help me and Thank you for your time. I am trying to do a simple parse of crime data returned by api (sanfrancisco.crimespotting.org) and running into a snag. Very simple code below.

<? 
$url = "http://sanfrancisco.crimespotting.org/crime-data?format=xml&count=5&dstart=2009-04-20";

$reports = new SimpleXMLElement($url, NULL, TRUE);

foreach ($reports-> report as $key => $value)
            {
            echo '<br />';
            echo '<b>Case #:</b> '.$reports -> report['case_number'];
            echo '<br />';
            echo '<b>Crime type</b> '.$reports -> report['crime_type'];
            echo '<br />';
            echo '<b>Date/Time:</b> '.$reports -> report['date_time'];
            echo '<br />';
            echo '<b>Date </b> '.$reports -> report['date'];
            echo '<br />';
            echo '<b>Time:</b> '.$reports -> report['time'];
            echo '<br />';
            echo '<b>More Info </b> '.$reports -> report['href'];
            echo '<br />';
            echo '<br />';
            echo '<br />';
            }
?>

The api returns 5 reports cause when I test the url in my browser I see 5 report tags returned. Here is the output from my browser.

            <reports>
            <report case_number="120220205" crime_type="Vehicle Theft" date_time="2012-07-17T23:10:00-07:00" date="Tuesday, Jul 17, 2012" time="11:10pm" lat="37.772441" lon="-122.412422" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858">STOLEN AUTOMOBILE</report>
            <report case_number="120560807" crime_type="Alcohol" date_time="2012-07-16T13:35:00-07:00" date="Monday, Jul 16, 2012" time="1:35pm" lat="37.783288" lon="-122.408954" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-16/Alcohol/334582">
            UNDER INFLUENCE OF ALCOHOL IN A PUBLIC PLACE (ARREST, BOOKED)
            </report>
            <report case_number="120559850" crime_type="Disturbing the Peace" date_time="2012-07-16T13:00:00-07:00" date="Monday, Jul 16, 2012" time="1:00pm" lat="37.778678" lon="-122.416545" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-16/Disturbing_The_Peace/334575">COMMITTING PUBLIC NUISANCE (ARREST, CITED)</report>
            <report case_number="120560653" crime_type="Theft" date_time="2012-07-16T12:35:00-07:00" date="Monday, Jul 16, 2012" time="12:35pm" lat="37.784644" lon="-122.414271" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-16/Theft/334580">PETTY THEFT SHOPLIFTING</report>
            <report case_number="120560700" crime_type="Theft" date_time="2012-07-16T12:00:00-07:00" date="Monday, Jul 16, 2012" time="12:00pm" lat="37.78966" lon="-122.400934" beat="" href="http://sanfrancisco.crimespotting.org/crime/2012-07-16/Theft/334581">GRAND THEFT BICYCLE</report>
            </reports>

As you can see (above) each report tag has a different case_number attribute (eg 120220205, 120560807 and so on). However, when I load the page this is the output I receive.

            Case #: 120220205
            Crime type Vehicle Theft
            Date/Time: 2012-07-17T23:10:00-07:00
            Date Tuesday, Jul 17, 2012
            Time: 11:10pm
            More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858



            Case #: 120220205
            Crime type Vehicle Theft
            Date/Time: 2012-07-17T23:10:00-07:00
            Date Tuesday, Jul 17, 2012
            Time: 11:10pm
            More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858



            Case #: 120220205
            Crime type Vehicle Theft
            Date/Time: 2012-07-17T23:10:00-07:00
            Date Tuesday, Jul 17, 2012
            Time: 11:10pm
            More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858



            Case #: 120220205
            Crime type Vehicle Theft
            Date/Time: 2012-07-17T23:10:00-07:00
            Date Tuesday, Jul 17, 2012
            Time: 11:10pm
            More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858



            Case #: 120220205
            Crime type Vehicle Theft
            Date/Time: 2012-07-17T23:10:00-07:00
            Date Tuesday, Jul 17, 2012
            Time: 11:10pm
            More Info http://sanfrancisco.crimespotting.org/crime/2012-07-17/Vehicle_Theft/320858

Since I am using a foreach loop to loop through the returned results, I don't know why the first case is being repeated 5 times, as I understand the foreach loop should automatically increment.

1
  • 2
    where are you using the key/value pair you're looping on? Commented Aug 4, 2012 at 0:15

5 Answers 5

3

Just replace $reports -> report in the loop by $value to get it work.

It seems SimpleXMLElement allows to use $reports->report as enumerable and as simple object for simplification in cases without multiple of the same tags.

That's most probably why you didn't get an error on this!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the added info. I was wondering why the code the poster used even gave the appearance of working.
3

You are not using the incremented data within the foreach loop. You want to do something like this instead:

foreach ($reports->report as $report)
{
    echo '<br />';
    echo '<b>Case #:</b> ' . $report['case_number'];
    // ...
}

In your code, you were iterating through $reports->report, as $key => $value but instead of using the iterated values ($key and $value), you were calling $reports->report, which does not change as the loop is iterated.

Comments

0

you are getting your foreach syntax wrong. Try replacing the $reports -> report inside the foreach block with $value

Comments

0

You are not referring to neither $key nor $value in the loop! You are always referring to $reports->report['case_number'], which is always the same.

EDIT: I forgot to mention you shoud iterate over $reports.

Comments

0

I want to thank everyone that chimed in so quickly to help. After reviewing the PHP docs some more (specifically, http://tinyurl.com/36a4aet), I found that I needed to use the children() method on $reports (as its the root tag) to get each report then for each report tag call the attributes() method to get the rest of the data for the report. The new code below seems to have done the trick :).

            foreach ($reports->children() as $node)
            {
                    $attribs = $node->attributes();
                    echo '<br />';
                    echo '<b>Case #:</b> '.$attribs["case_number"];
                    echo '<br />';
                    echo '<b>Crime type:</b> '.$attribs["crime_type"];
                    echo '<br />';
                    echo '<b>Date/Time:</b> '.$attribs["date_time"];
                    echo '<br />';
                    echo '<b>Date </b>: '.$attribs["date"];
                    echo '<br />';
                    echo '<b>Time:</b> '.$attribs["time"];
                    echo '<br />';
                    echo '<b>More Info: </b> <a href='.$attribs["href"].'>'.$attribs["href"].'</a>';
                    echo '<br />';
                    echo '<br />';
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.