Texan78 Posted May 12, 2014 Share Posted May 12, 2014 Hello all, I have a very simple issue that I am having problems with. I have done this several times but not sure what I am missing. Probably just a brain fart and need a second set of eyes. I have a XML URL I am parsing with SimpleXML and for some reason it isn't pulling the data now that I have included it in a loop. Just trying to pull each entry and put it in its own table. This is something I have done over and over but I am missing something. Only listing the relevant code. The echo statement is further down the page and not included in the snippet. //Set path to data files //Incidents XML URL $incident_data = "http://dfwtraffic.dot.state.tx.us/DalTrans/GetFile.aspx?FileName=MapBurnerOutput/Incidents.rss&Exact=true"; //Lets load the XML file for the loop $xml = simplexml_load_file($incident_data); //Set initial output to false $tData = false; foreach($xml->channel as $channel) { $incident_data_title = $channel->item['title']; $incident_data_desc = $channel->item['description']; $incident_data_date = $channel['pubDate']; $incident_data_updated = DATE("D, M d, g:i a", STRTOTIME($incident_data_date)); // construct data for table display $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n"; $tData .= "<tbody>\n"; $tData .= " <tr><td style='{$td1Style}'>Updated: {$incident_data_updated}</td></tr>\n"; $tData .= " <tr>\n"; $tData .= " <td style='{$td2Style}'><strong>Incident:</strong>{$incident_data_title}</td>\n"; $tData .= " </tr>\n"; $tData .= " <tr><td style='{$td3Style}'> </td></tr>\n"; $tData .= " <tr><td style='{$td4Style}'><strong>Description:</strong>{$incident_data_desc}</td></tr>\n"; $tData .= "</tbody>\n"; $tData .= "</table>\n"; } Here is a sample of the XML file I am parsing. Very simple and not complicated. I can parse data outside the loop but, once I add it inside the loop it doesn't pull anything. <?xml version="1.0" encoding="utf-8"?> <rss version="2.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:c2c="http://its.gov/c2c_icd" xmlns:tti="urn:tti"> <channel> <title>Dallas Area Freeway Incidents </title> <link>http://dfwtraffic.dot.state.tx.us</link> <language>en-us</language> <category domain="dfwtraffic.dot.state.tx.us">LaneClosures</category> <lastBuildDate>5/11/2014 8:35:35 PM</lastBuildDate> <pubDate>5/11/2014 8:35:35 PM</pubDate> <image> <url>http://dfwtraffic.dot.state.tx.us/images/daltranslogo.gif</url> <link>http://dfwtraffic.dot.state.tx.us</link> </image> <item> <guid isPermaLink="false">200b64713c084791a12e34245791a575</guid> <title>VehicleCollision - Westbound IH 30 @ Westmoreland Rd</title> <link>http://dfwtraffic.dot.state.tx.us</link> <description><![CDATA[<div text-align:left;'><b>Status:</b> Verified at 8:31 PM on Sunday, May 11<br/><b>Affected Lanes:</b> Lane 1, Lane 2, Lane 3</div>]]></description> </item> </channel> </rss> What am I missing? -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/288428-simple-xml-entry-problem/ Share on other sites More sharing options...
Barand Posted May 12, 2014 Share Posted May 12, 2014 Title, description and pubDate are child elements, not attributes foreach($xml->channel as $channel) { $incident_data_title = $channel->item->title; $incident_data_desc = $channel->item->description; $incident_data_date = $channel->pubDate; $incident_data_updated = DATE("D, M d, g:i a", STRTOTIME($incident_data_date)); echo 'Title: ' . $incident_data_title . '<br>'; echo 'Desc: ' . $incident_data_desc . '<br>'; echo 'Date: ' . $incident_data_date . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/288428-simple-xml-entry-problem/#findComment-1479234 Share on other sites More sharing options...
Texan78 Posted May 12, 2014 Author Share Posted May 12, 2014 Thanks Barand, I realized that and got it sorted before I saw this. I was coming back to update it. Now the problem is, it's not looping through each entry, it only displays the first one. //Lane closure data $lane_data = "http://dfwtraffic.dot.state.tx.us/DalTrans/GetFile.aspx?FileName=MapBurnerOutput/LaneClosures.rss&Exact=true"; //Lets load the XML file for the loop $xml = simplexml_load_file($lane_data); //Set initial output to false $tData = false; foreach($xml->channel as $channel) { $incident_data_title = $channel->item->title; $incident_data_desc = $channel->item->description; $incident_data_date = $channel->pubDate; $incident_data_updated = DATE("D, M d, g:i a", STRTOTIME($incident_data_date)); // construct data for table display $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n"; $tData .= "<tbody>\n"; $tData .= " <tr><td style='{$td1Style}'>Updated: {$incident_data_updated}</td></tr>\n"; $tData .= " <tr>\n"; $tData .= " <td style='{$td2Style}'><strong>Roadway:</strong> {$incident_data_title}</td>\n"; $tData .= " </tr>\n"; $tData .= " <tr><td style='{$td3Style}'> </td></tr>\n"; $tData .= " <tr><td style='{$td4Style}'><strong>Description:</strong> {$incident_data_desc}</td></tr>\n"; $tData .= "</tbody>\n"; $tData .= "</table>\n"; } echo $tData; Quote Link to comment https://forums.phpfreaks.com/topic/288428-simple-xml-entry-problem/#findComment-1479243 Share on other sites More sharing options...
Barand Posted May 12, 2014 Share Posted May 12, 2014 You need to loop throught the items, not the channel $xml = simplexml_load_file($lane_data); $incident_data_date = $channel->pubDate; $incident_data_updated = DATE("D, M d, g:i a", STRTOTIME($incident_data_date)); foreach($xml->channel->item as $item) { $incident_data_title = $item->title; $incident_data_desc = $item->description; echo 'Title: ' . $incident_data_title . '<br>'; echo 'Desc: ' . $incident_data_desc . '<br>'; echo 'Date: ' . $incident_data_date . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/288428-simple-xml-entry-problem/#findComment-1479249 Share on other sites More sharing options...
Solution Texan78 Posted May 12, 2014 Author Solution Share Posted May 12, 2014 Thanks that is what I thought. The issue I was fighting with that is when the data is outside the loop it doesn't pull the data. Not sure why as they doesn't need to be in it as you're right. Is this right? //Lets load the XML file for the loop $xml = simplexml_load_file($lane_data); $incident_data_date = $channel->pubDate; $incident_data_updated = DATE("D, M d, g:i A", STRTOTIME($incident_data_date)); //Set initial output to false $tData = false; foreach($xml->channel->item as $item) { $incident_data_title = $item->title; $incident_data_desc = $item->description; // construct data for table display $tData .= "<table style='{$tableStyle}' cellpadding='0' cellspacing='0'>\n"; $tData .= "<tbody>\n"; $tData .= " <tr><td style='{$td1Style}'>Updated: {$incident_data_updated}</td></tr>\n"; $tData .= " <tr>\n"; $tData .= " <td style='{$td2Style}'><strong>Roadway:</strong> {$incident_data_title}</td>\n"; $tData .= " </tr>\n"; $tData .= " <tr><td style='{$td3Style}'> </td></tr>\n"; $tData .= " <tr><td style='{$td4Style}'><strong>Description:</strong> {$incident_data_desc}</td></tr>\n"; $tData .= "</tbody>\n"; $tData .= "</table>\n"; } echo $tData; Quote Link to comment https://forums.phpfreaks.com/topic/288428-simple-xml-entry-problem/#findComment-1479278 Share on other sites More sharing options...
Barand Posted May 12, 2014 Share Posted May 12, 2014 I'd move the <table> and <tbody> to before the while loop and move the </table> to after the loop. Unless, of course, you specifically want each item in its own table and not just a row in a single table. Quote Link to comment https://forums.phpfreaks.com/topic/288428-simple-xml-entry-problem/#findComment-1479279 Share on other sites More sharing options...
Texan78 Posted May 12, 2014 Author Share Posted May 12, 2014 Yes, that is correct. I want each entry its own table. This would probably be less painless if each entry included its own pubDate in stead of just for the overall feed. Quote Link to comment https://forums.phpfreaks.com/topic/288428-simple-xml-entry-problem/#findComment-1479280 Share on other sites More sharing options...
Texan78 Posted May 12, 2014 Author Share Posted May 12, 2014 (edited) I got it sorted. I had another brain fart moment. I need to change this. channel->pubDate; to this $xml->channel->pubDate; Edited May 12, 2014 by Texan78 Quote Link to comment https://forums.phpfreaks.com/topic/288428-simple-xml-entry-problem/#findComment-1479285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.