PHP foreach loop not incrementing
PHP foreach loop not incrementing
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.
report as $key => $value) { echo '
'; echo 'Case #: '.$reports -> report['case_number']; echo '
'; echo 'Crime type '.$reports -> report['crime_type']; echo '
'; echo 'Date/Time: '.$reports -> report['date_time']; echo '
'; echo 'Date '.$reports -> report['date']; echo '
'; echo 'Time: '.$reports -> report['time']; echo '
'; echo 'More Info '.$reports -> report['href']; echo '
'; echo '
'; echo '
'; } ?>
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.
STOLEN AUTOMOBILE UNDER INFLUENCE OF ALCOHOL IN A PUBLIC PLACE (ARREST, BOOKED) COMMITTING PUBLIC NUISANCE (ARREST, CITED) PETTY THEFT SHOPLIFTING GRAND THEFT BICYCLE
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.
Answer by Pevara for PHP foreach loop not incrementing
you are getting your foreach syntax wrong. Try replacing the $reports -> report
inside the foreach block with $value
Answer by Lars Knickrehm for PHP foreach loop not incrementing
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!
Answer by Andrew for PHP foreach loop not incrementing
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 '
'; echo 'Case #: ' . $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.
Answer by Claudix for PHP foreach loop not incrementing
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.
Answer by Moe k for PHP foreach loop not incrementing
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 '
'; echo 'Case #: '.$attribs["case_number"]; echo '
'; echo 'Crime type: '.$attribs["crime_type"]; echo '
'; echo 'Date/Time: '.$attribs["date_time"]; echo '
'; echo 'Date : '.$attribs["date"]; echo '
'; echo 'Time: '.$attribs["time"]; echo '
'; echo 'More Info: '.$attribs["href"].''; echo '
'; echo '
'; }
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment