Texan78 Posted December 19, 2014 Share Posted December 19, 2014 Hello, I have this very frustrating problem I'm trying to make a dynamic table with only 5 columns per row. So every 5 items, I need a new row. I have tried many different examples with no success. What is the best way to approach this? Here is what I am working with, this doesn't show what I have tried, just what I am working with at the moment which of course, just outputs it one column per row. http://www.mesquiteweather.net/inc/inc-legend.php // Lets parse the data $entries = simplexml_load_file($data); if(count($entries)): //Registering NameSpace $entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom'); $result = $entries->xpath("//prefix:entry"); foreach ($result as $entry): $event = $entry->children("cap", true)->event; // Set the alert colors for the legend include ('../inc-NWR-alert-colors.php'); // Lets creat some styles for the list $spanStyle = "background-color:{$alertColor};border:solid 1px #333;width:15px;height:10px;display:inline-block;'> </span><span style='font-size:12px;color:#555;"; $legend .= "<table>"; $legend .= "<tr>"; $legend .= "<td> <span style='$spanStyle'> $event</span></td>"; $legend .= "</tr>"; $legend .= "</table>"; endforeach; endif; echo $legend; -Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/ Share on other sites More sharing options...
cyberRobot Posted December 19, 2014 Share Posted December 19, 2014 My current preference is to read all the values into an array. Then split the array with array_chunk() and loop through the results. http://php.net/manual/en/function.array-chunk.php Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500090 Share on other sites More sharing options...
Solution Frank_b Posted December 19, 2014 Solution Share Posted December 19, 2014 (edited) or like this: $legend .= "<table>"; $legend .= "<tr>"; $i = 1; foreach ($result as $entry) { $event = $entry->children("cap", true)->event; $legend .= "<td> <span style='$spanStyle'> $event</span></td>"; if($i % 5 == 0) $legend .= "</tr><tr>"; $i++; } $legend .= "</tr>"; $legend .= "</table>"; Edited December 19, 2014 by Frank_b Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500091 Share on other sites More sharing options...
Texan78 Posted December 19, 2014 Author Share Posted December 19, 2014 I have striped everything away and just broke it down to what I needed and started small and worked up from there. I have tried both methods, both of them previously actually and referencing the php.net manuals and I know this is what I need but, foreach statements really throws me for a loop, no pun intended. So I have tried this. //Set path to data file $data = "http://alerts.weather.gov/cap/".$zone.".php?x=1"; // Lets parse the data $entries = simplexml_load_file($data); if(count($entries)): //Registering NameSpace $entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom'); $result = $entries->xpath("//prefix:entry"); foreach ($result as $entry): $event = $entry->children("cap", true)->event; $hazard_array = array('911 Telephone Outage', 'Administrative Message', 'Air Quality Alert', 'Air Stagnation Advisory', 'Arroyo and Small Stream Flood Advisory', 'Ashfall Advisory', 'Ashfall Warning', 'Avalanche Advisory', 'Avalanche Warning', 'Avalanche Watch', 'Beach Hazards Statement', 'Blizzard Warning', 'Blizzard Warning', 'Blizzard Watch', 'Blowing Dust Advisory', 'Brisk Wind Advisory', 'Child Abduction Emergency', 'Civil Danger Warning', 'Civil Emergency Message', 'Coastal Flood Advisory', 'Coastal Flood Statement', 'Coastal Flood Warning', 'Coastal Flood Watch', 'Dense Fog Advisory', 'Dense Smoke Advisory', 'Dust Storm Warning', 'Earthquake Warning', 'Evacuation - Immediate', 'Excessive Heat Warning', 'Excessive Heat Watch', 'Extreme Cold Warning', 'Extreme Cold Watch', 'Extreme Fire Danger', 'Extreme Wind Warning', 'Fire Warning', 'Fire Weather Watch', 'Flash Flood Statement', 'Flash Flood Warning', 'Flash Flood Watch', 'Flood Advisory', 'Flood Statement', 'Flood Warning', 'Flood Watch', 'Freeze Warning', 'Freeze Watch', 'Freezing Fog Advisory', 'Freezing Rain Advisory', 'Freezing Spray Advisory', 'Frost Advisory', 'Gale Warning', 'Gale Watch', 'Hard Freeze Warning', 'Hard Freeze Watch', 'Hazardous Materials Warning', 'Hazardous Seas Warning', 'Hazardous Seas Watch', 'Hazardous Weather Outlook', 'Heat Advisory', 'Heavy Freezing Spray Warning', 'Heavy Freezing Spray Watch', 'High Surf Advisory', 'High Surf Warning', 'High Wind Warning', 'High Wind Watch', 'Hurricane Force Wind Warning', 'Hurricane Force Wind Watch', 'Hurricane Local Statement', 'Hurricane Warning', 'Hurricane Watch', 'Hydrologic Advisory', 'Hydrologic Outlook', 'Ice Storm Warning', 'Lake Effect Snow Advisory', 'Lake Effect Snow Warning', 'Lake Effect Snow Watch', 'Lake Wind Advisory', 'Lakeshore Flood Advisory', 'Lakeshore Flood Statement', 'Lakeshore Flood Warning', 'Lakeshore Flood Watch', 'Law Enforcement Warning', 'Local Area Emergency', 'Low Water Advisory', 'Marine Weather Statement', 'Nuclear Power Plant Warning', 'Radiological Hazard Warning', 'Red Flag Warning', 'Rip Current Statement', 'Severe Thunderstorm Warning', 'Severe Thunderstorm Watch', 'Severe Weather Statement', 'Shelter In Place Warning', 'Short Term Forecast', 'Small Craft Advisory', 'Small Craft Advisory For Hazardous Seas', 'Small Craft Advisory For Rough Bar', 'Small Craft Advisory For Winds', 'Small Stream Flood Advisory', 'Special Marine Warning', 'Special Weather Statement', 'Storm Warning', 'Storm Watch', 'Tornado Warning', 'Tornado Watch', 'Tropical Depression Local Statement', 'Tropical Storm Local Statement', 'Tropical Storm Warning', 'Tropical Storm Watch', 'Tsunami Advisory', 'Tsunami Warning', 'Tsunami Watch', 'Typhoon Local Statement', 'Typhoon Warning', 'Typhoon Watch', 'Urban and Small Stream Flood Advisory', 'Volcano Warning', 'Wind Advisory', 'Wind Chill Advisory', 'Wind Chill Warning', 'Wind Chill Watch', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory'); $rows = array_chunk($hazard_array, 5); print "<table>\n"; foreach ($rows as $row) { print "<tr>\n"; foreach ($row as $event) { print "<td>" . $event . "</td>\n"; } print "</tr>\n"; } print "</table>\n"; and tried this. // Lets parse the data $entries = simplexml_load_file($data); if(count($entries)): //Registering NameSpace $entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom'); $result = $entries->xpath("//prefix:entry"); // Set the alert colors for the legend include ('../inc-NWR-alert-colors.php'); // Lets creat some styles for the list $spanStyle = "background-color:{$alertColor};border:solid 1px #333;width:15px;height:10px;display:inline-block;'> </span><span style='font-size:12px;color:#555;"; $legend .= "<table>"; $legend .= "<tr>"; $i = 1; foreach ($result as $entry) { $event = $entry->children("cap", true)->event; $legend .= "<td> <span style='$spanStyle'> $event</span></td>"; if($i % 5 == 0) $legend .= "</tr><tr>"; $i++; } $legend .= "</tr>"; $legend .= "</table>"; endforeach; endif; echo $legend; Nether one of them display anything. What is it I am missing. Like I said before the whole $i++ == 0 banana purple = peanut butter is really confusing to me as much as I am trying to understand it as I am not sure what the some of the variables are since to me that don't look defined, they are just random variables or are they? -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500102 Share on other sites More sharing options...
Frank_b Posted December 19, 2014 Share Posted December 19, 2014 can you post (a short version of ) the xml file? Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500103 Share on other sites More sharing options...
Texan78 Posted December 19, 2014 Author Share Posted December 19, 2014 Well I am making some progress. I least have it laying out like I need it to. Only issue now is it is not pulling the colors for the span style from the included array like it is suppose to as shown in the link in my first post. It also doesn't appear to be only showing the events from the XML file it is reading from. It shows everything from the array. Here is what I am working with now. I have a feeling it is just placement issue with things in the wrong place and the endif and endforeach statements. Here is the XML I am working with as an example. http://alerts.weather.gov/cap/ca.atom <?php ini_set('display_errors','1'); $zone = 'ca'; // Put the the two letter abbreviation of your state //Set path to data file $data = "http://alerts.weather.gov/cap/".$zone.".php?x=1"; $entries = simplexml_load_file($data); if(count($entries)): //Registering NameSpace $entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom'); $result = $entries->xpath("//prefix:entry"); foreach ($result as $entry): $event[] = $entry->children("cap", true)->event; endforeach; endif; // Set the alert colors for the legend include ('../inc-NWR-alert-colors.php'); // Lets creat some styles for the list $spanStyle = "background-color:{$alertColor};border:solid 1px #333;width:15px;height:10px;display:inline-block;'> </span><span style='font-size:12px;color:#555;"; $hazard_array = array('911 Telephone Outage', 'Administrative Message', 'Air Quality Alert', 'Air Stagnation Advisory', 'Arroyo and Small Stream Flood Advisory', 'Ashfall Advisory', 'Ashfall Warning', 'Avalanche Advisory', 'Avalanche Warning', 'Avalanche Watch', 'Beach Hazards Statement', 'Blizzard Warning', 'Blizzard Warning', 'Blizzard Watch', 'Blowing Dust Advisory', 'Brisk Wind Advisory', 'Child Abduction Emergency', 'Civil Danger Warning', 'Civil Emergency Message', 'Coastal Flood Advisory', 'Coastal Flood Statement', 'Coastal Flood Warning', 'Coastal Flood Watch', 'Dense Fog Advisory', 'Dense Smoke Advisory', 'Dust Storm Warning', 'Earthquake Warning', 'Evacuation - Immediate', 'Excessive Heat Warning', 'Excessive Heat Watch', 'Extreme Cold Warning', 'Extreme Cold Watch', 'Extreme Fire Danger', 'Extreme Wind Warning', 'Fire Warning', 'Fire Weather Watch', 'Flash Flood Statement', 'Flash Flood Warning', 'Flash Flood Watch', 'Flood Advisory', 'Flood Statement', 'Flood Warning', 'Flood Watch', 'Freeze Warning', 'Freeze Watch', 'Freezing Fog Advisory', 'Freezing Rain Advisory', 'Freezing Spray Advisory', 'Frost Advisory', 'Gale Warning', 'Gale Watch', 'Hard Freeze Warning', 'Hard Freeze Watch', 'Hazardous Materials Warning', 'Hazardous Seas Warning', 'Hazardous Seas Watch', 'Hazardous Weather Outlook', 'Heat Advisory', 'Heavy Freezing Spray Warning', 'Heavy Freezing Spray Watch', 'High Surf Advisory', 'High Surf Warning', 'High Wind Warning', 'High Wind Watch', 'Hurricane Force Wind Warning', 'Hurricane Force Wind Watch', 'Hurricane Local Statement', 'Hurricane Warning', 'Hurricane Watch', 'Hydrologic Advisory', 'Hydrologic Outlook', 'Ice Storm Warning', 'Lake Effect Snow Advisory', 'Lake Effect Snow Warning', 'Lake Effect Snow Watch', 'Lake Wind Advisory', 'Lakeshore Flood Advisory', 'Lakeshore Flood Statement', 'Lakeshore Flood Warning', 'Lakeshore Flood Watch', 'Law Enforcement Warning', 'Local Area Emergency', 'Low Water Advisory', 'Marine Weather Statement', 'Nuclear Power Plant Warning', 'Radiological Hazard Warning', 'Red Flag Warning', 'Rip Current Statement', 'Severe Thunderstorm Warning', 'Severe Thunderstorm Watch', 'Severe Weather Statement', 'Shelter In Place Warning', 'Short Term Forecast', 'Small Craft Advisory', 'Small Craft Advisory For Hazardous Seas', 'Small Craft Advisory For Rough Bar', 'Small Craft Advisory For Winds', 'Small Stream Flood Advisory', 'Special Marine Warning', 'Special Weather Statement', 'Storm Warning', 'Storm Watch', 'Tornado Warning', 'Tornado Watch', 'Tropical Depression Local Statement', 'Tropical Storm Local Statement', 'Tropical Storm Warning', 'Tropical Storm Watch', 'Tsunami Advisory', 'Tsunami Warning', 'Tsunami Watch', 'Typhoon Local Statement', 'Typhoon Warning', 'Typhoon Watch', 'Urban and Small Stream Flood Advisory', 'Volcano Warning', 'Wind Advisory', 'Wind Chill Advisory', 'Wind Chill Warning', 'Wind Chill Watch', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory'); $rows = array_chunk($hazard_array, 5); print "<table>\n"; foreach ($rows as $row) { print "<tr>\n"; foreach ($row as $event) { print "<td><span style='$spanStyle'> " . $event . "</span></td>\n"; } print "</tr>\n"; } print "</table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500110 Share on other sites More sharing options...
Barand Posted December 19, 2014 Share Posted December 19, 2014 $spanStyle = "background-color:{$alertColor};border:solid 1px #333;width:15px;height:10px;display:inline-block;'> </span><span style='font-size:12px;color:#555;"; How are you planning on setting the different values for $alertColor each time you output a <span>? Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500113 Share on other sites More sharing options...
Texan78 Posted December 20, 2014 Author Share Posted December 20, 2014 How are you planning on setting the different values for $alertColor each time you output a <span>? Was curious when someone would ask...LoL. That variable is set in an array in another file called like so. // Set the alert colors for the legend include ('../inc-NWR-alert-colors.php'); I did it like that because it is a very large array. If you see the array above $hazard_array it has that many entries only done like this. $alertColor = ''; switch(TRUE) { case 'Air Quality Alert': $alertColor = 'rgba(128, 128, 128, 0.4)'; break; case 'Child Abduction Emergency': $alertColor = 'rgba(255, 215, 0, 0.4)'; break; case 'Fire Weather Watch': $alertColor = 'rgba(255, 222, 173, 0.4)'; break; case 'Red Flag Warning': $alertColor = 'rgba(255, 20, 147, 0.4)'; break; case 'Special Weather Statement': $alertColor = 'rgba(255, 228, 181, 0.4)'; break; Which works great because I use it in other scripts as well on different pages so it is kind of a global array if you would call it that. So since it is so big and it would take up a lot of room I just put it in its own file and call it where I need it. That was a tip I was recommended to me a couple years ago. Is that a bad thing? Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500116 Share on other sites More sharing options...
Texan78 Posted December 20, 2014 Author Share Posted December 20, 2014 My current preference is to read all the values into an array. Then split the array with array_chunk() and loop through the results. http://php.net/manual/en/function.array-chunk.php How would I do this from the output of the variable created from the SimpleXML? I think the easiest method is what Frank_b suggested but, I can't get it to work. I just need to take the output of $event and create 5 columns and start a new row if need be. Then remove the duplicates from $event. Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500128 Share on other sites More sharing options...
Texan78 Posted December 20, 2014 Author Share Posted December 20, 2014 (edited) or like this: $legend .= "<table>"; $legend .= "<tr>"; $i = 1; foreach ($result as $entry) { $event = $entry->children("cap", true)->event; $legend .= "<td> <span style='$spanStyle'> $event</span></td>"; if($i % 5 == 0) $legend .= "</tr><tr>"; $i++; } $legend .= "</tr>"; $legend .= "</table>"; Ok so I finally got this to work but, now I can't get the alert colors array that is added via file include. I have tried it both inside and outside of the endif and endforeach but they don't show up anymore. Any suggestions? // Lets parse the data$entries = simplexml_load_file($data); if(count($entries)): //Registering NameSpace $entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom'); $result = $entries->xpath("//prefix:entry"); foreach ($result as $entry): $event = $entry->children("cap", true)->event; // Set the alert colors for the legend include ('../inc-NWR-alert-colors.php'); endforeach; endif; // Lets creat some styles for the list $spanStyle = "background-color:{$alertColor};border:solid 1px #333;width:15px;height:10px;display:inline-block;'> </span><span style='font-size:12px;color:#555;"; $legendStyle = "margin:10px auto 10px auto;"; $legend .= "<table style='$legendStyle' cellspacing='5px'>"; $legend .= "<tr>"; $i = 1; foreach ($result as $entry) { $event = $entry->children("cap", true)->event; $legend .= "<td> <span style='$spanStyle'> $event</span></td>"; if($i % 5 == 0) $legend .= "</tr><tr>"; $i++; } $legend .= "</tr>"; $legend .= "</table>"; echo $legend; Edited December 20, 2014 by Texan78 Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500135 Share on other sites More sharing options...
Texan78 Posted December 20, 2014 Author Share Posted December 20, 2014 I got the color array sorted out. Last thing I am struggling with is how to remove the duplicate entries. I have been researching a lot on it but not solid that works in my situation. As you can see there is duplicates that need to be removed. http://www.mesquiteweather.net/inc/inc-legend.php // Lets parse the data $entries = simplexml_load_file($data); if(count($entries)): //Registering NameSpace $entries->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom'); $result = $entries->xpath("//prefix:entry"); foreach ($result as $entry): $event = $entry->children("cap", true)->event; // Set the alert colors for the legend include ('../inc-NWR-alert-colors.php'); $spanStyle = "background-color:{$alertColor};border:solid 1px #333;width:15px;height:10px;display:inline-block;'> </span><span style='font-size:12px;color:#555;"; endforeach; endif; // Lets creat some styles for the list $legendStyle = "margin:10px auto 10px auto;"; $legend .= "<table style='$legendStyle' cellspacing='5px'>"; $legend .= "<tr>"; $i = 1; foreach ($result as $entry) { $event = $entry->children("cap", true)->event; //Set the alert colors for the legend include ('../inc-NWR-alert-colors.php'); $spanStyle = "background-color:{$alertColor};border:solid 1px #333;width:15px;height:10px;display:inline-block;'> </span><span style='font-size:12px;color:#555;"; $legend .= "<td> <span style='$spanStyle'> $event</span></td>"; if($i % 5 == 0) $legend .= "</tr><tr>"; $i++; } $legend .= "</tr>"; $legend .= "</table>"; echo $legend; Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500146 Share on other sites More sharing options...
Barand Posted December 20, 2014 Share Posted December 20, 2014 It's working for the wrong reasons so it's teaching you the wrong way to use a switch. This time it works, sure, but next time it won't and you won't understand why because "it worked that other time". How prophetic is that ? Was curious when someone would ask...LoL. That variable is set in an array in another file called like so. // Set the alert colors for the legend include ('../inc-NWR-alert-colors.php'); I did it like that because it is a very large array. If you see the array above $hazard_array it has that many entries only done like this. $alertColor = ''; switch(TRUE) { case 'Air Quality Alert': $alertColor = 'rgba(128, 128, 128, 0.4)'; break; case 'Child Abduction Emergency': $alertColor = 'rgba(255, 215, 0, 0.4)'; break; case 'Fire Weather Watch': $alertColor = 'rgba(255, 222, 173, 0.4)'; break; case 'Red Flag Warning': $alertColor = 'rgba(255, 20, 147, 0.4)'; break; case 'Special Weather Statement': $alertColor = 'rgba(255, 228, 181, 0.4)'; break; Which works great because I use it in other scripts as well on different pages so it is kind of a global array if you would call it that. So since it is so big and it would take up a lot of room I just put it in its own file and call it where I need it. That was a tip I was recommended to me a couple years ago. Is that a bad thing? Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500190 Share on other sites More sharing options...
Barand Posted December 20, 2014 Share Posted December 20, 2014 Why don't you incorporate the colore into your hazards array. For example $hazard_array = array ( array ('Air Quality Alert', 'rgba(128, 128, 128, 0.4)'), array ('Child Abduction Emergency', 'rgba(255, 215, 0, 0.4)'), array ('Fire Weather Watch', 'rgba(255, 222, 173, 0.4)'), array ('Red Flag Warning', 'rgba(255, 20, 147, 0.4)'), array ('Special Weather Statement', 'rgba(255, 228, 181, 0.4)') ); foreach ($hazard_array as $ha) { $spanStyle = "background-color:{$ha[1]};border:solid 1px #333;width:15px;height:10px;display:inline-block;"; echo "<span style='$spanStyle'> </span> {$ha[0]}<br>"; } Gives Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500198 Share on other sites More sharing options...
Texan78 Posted December 20, 2014 Author Share Posted December 20, 2014 How prophetic is that ? Not sure what you mean, it still works. I had it placed in the wrong spot. Move it to where it was suppose to be and works fine now. No issues, nothing wrong with it. Why don't you incorporate the colore into your hazards array. Simplicity, I would rather not have 6+ array files of 400+ lines of code. The one I have now is global and is shared among several other scripts on the page. There are no variable conflicts because the scripts are never called together on the same page. This is the final results and works great now. $dom = new DOMDocument(); $dom->load($data); // Create an array of unique events $events = []; foreach ($dom->getElementsByTagNameNS("urn:oasis:names:tc:emergency:cap:1.1", "event") as $event) { $events[$event->C14N()] = $event; } // Lets creat some styles for the list $legendStyle = "margin:10px auto 10px auto;"; $legend .= "<table style='$legendStyle' cellspacing='5px'>"; $legend .= "<tr>"; $i = 1; foreach ($events as $event) { //Set the alert colors for the legend include ('../inc-alert-colors.php'); $spanStyle = "background-color:{$alertColor};border:solid 1px #333;width:15px;height:10px;display:inline-block;'> </span><span style='font-size:12px;color:#555;"; $legend .= "<td> <span style='$spanStyle'> {$event->nodeValue}</span></td>"; if($i % 5 == 0) $legend .= "</tr><tr>"; $i++; } $legend .= "</tr>"; $legend .= "</table>"; echo $legend; Quote Link to comment https://forums.phpfreaks.com/topic/293183-creating-a-dynamic-table-with-5-columns-per-row/#findComment-1500210 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.