Jump to content

Texan78

Members
  • Posts

    272
  • Joined

  • Last visited

Everything posted by Texan78

  1. I have tried your suggestion but then it doesn't show any color background. I have tried this, which works... switch($event->nodeValue) { Problem I am having now is, it should only be showing one alert but it shows multiple alerts if they are in the array. They are listed in the array by the order in which I wanted them to display if they exist in the RSS feed I am parsing. So for example, it should only be displaying Flood Warning and not Flash Flood Watch. Here is the relevant code.... $alertValues = array('Tornado Warning', 'Severe Thunderstorm Warning', 'Tornado Watch', 'Severe Thunderstorm Watch', 'Flash Flood Warning', 'Flood Warning', 'Flash Flood Watch', 'Flood Watch', 'Flood Advisory', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory', 'Special Weather Statement'); $events = array_intersect($alertValues, $events); // Set the alert colors for the banner background switch($event->nodeValue) { case 'Tornado Warning': $alertColor = 'rgba(255, 0, 0, 0.4)'; break; case 'Severe Thunderstorm Warning': $alertColor = 'rgba(255, 165, 0, 0.4)'; break; case 'Tornado Watch': $alertColor = 'rgba(255, 255, 0, 0.4)'; break; case 'Severe Thunderstorm Watch': $alertColor = 'rgba(219, 112, 147, 0.4)'; break; case 'Flash Flood Warning': $alertColor = 'rgba(139, 0, 0, 0.4)'; break; case 'Flash Flood Watch': $alertColor = 'rgba(46, 139, 87, 0.4)'; break; case 'Flood Warning': $alertColor = 'rgba(0, 255, 0, 0.4)'; break; case 'Flood Watch': $alertColor = 'rgba(46, 139, 87, 0.4)'; break; case 'Flood Advisory': $alertColor = 'rgba(0, 255, 127, 0.4)'; break; case 'Winter Storm Warning': $alertColor = 'rgba(255, 105, 180, 0.4)'; break; case 'Winter Storm Watch': $alertColor = 'rgba(70, 130, 180, 0.4)'; break; case 'Winter Weather Advisory': $alertColor = 'rgba(123, 104, 238, 0.4)'; break; case 'Special Weather Statement': $alertColor = 'rgba(255, 228, 181, 0.4)'; } // Lets assign some styles for the banner $bannerStyle = '"color:' . $alertText . '; width:100%; border-top-style:solid; border-bottom-style:solid; border-color:#FFF; border-width:1px; font-size:12px; text-shadow: 0 1px 1px #111; font-weight:700; font-family: Arial,Helvetica,sans-serif; text-align: center; background-color:' . $alertColor . '; margin-bottom:5px; padding: .2em 0em .2em 0em"'; // Lets assembly the banners to display foreach ($events as $message) { echo '<div style=' . $bannerStyle . '> <strong>WEATHER BROADCAST ALERT - ' . $message . '</strong>... Dallas County, listen now to our LIVE NOAA Weather Radio Stream <a href=" ' . $feed . '" class="NWRLink" onclick="return popup(this, \'noaa\')" title="Live NOAA Radio For Dallas County">[Click here to listen]</a> </div>'; } I think it is because of this below that is at the end of the code but I am not sure how to change that to only display one. I think the foreach statement makes it so it displays each value in the array. So how could I change that so it only shows the one with the most priority, priority is set in the array as the first one already? foreach ($events as $message) {
  2. Ah that makes perfect sense. I didn't think about the value being a boolean. Thank you very much for pointing that out. I was able to finish what I was needing to do with that value. Just to check my work for what I needed it for. Would this be correct? It seems to work and it is light weight. if ($status == "true") { echo $notify; } Meaning that if it is true, display the contents of the $notify variable. Seems to work through testing. Would this be correct, seeing any problems with that? -Thanks
  3. Hello, I have ran into a snag for something that is very elementary I am doing. It has been a while since I have worked with json and php so it could just be a simple lapse in memory. I have a json string I am trying to parse and this is the output of the string, very simple, cut and dry. All I need from that string is the "active" value. This is the very simple php code I have to parse it, which in the past works great and as outlined later works. <?php $jsonPath = "REMOVED_URL_BUT_GOES_HERE"; // URL to json data // Lets get and parse the data and create the variables $json_string = file_get_contents($jsonPath); $parsed_json = json_decode($json_string); $status = $parsed_json->active; echo $status; ?> So with the above php code when I echo the variable status, it should return the value as false but, instead I get nothing. Now as I said I would outline later, to troubleshoot and test if I do echo $json_string; then it outputs the entire string as it should so I know it is getting the contents. For some reason that I am simply overlooking it is not pulling the value for active for the $status variable. What am I missing or overlooking? -Thanks!
  4. Hello, I have a small script that depending on the title of the div the background has a different color. This use to work fine but, all of a sudden it just shows the color of the first case statement in the switch. Any suggestions what might be causing this? The title are based from an RSS feed that is parsed just for a little background on what is displayed and when. As you can see from the screen shot, this should be greenish color but, it is showing red and is taking the color of the first case. I tested this by removing the first one and then it shows yellow which is the second one. So that is how I determined it is not going down the line to display the correct color for the correct title. It is just stopping at the first title regardless if it is correct and just showing the first color. //Set path to data file #$data = "http://alerts.weather.gov/cap/".$zone.".atom"; $data = "http://alerts.weather.gov/cap/wwaatmget.php?x=".$zone."&y=1"; ## End Configurable data ## $alertColor = ' '; $alertText = '#FFF'; // get path info & protect for cross-site scripting vulnerability $sri = ($_SERVER['REQUEST_URI']) ? str_replace('#SA', '', htmlspecialchars(strip_tags($_SERVER['REQUEST_URI']))) : ''; // Lets parse the feed $dom = new DOMDocument(); $dom->load($data); $events = []; foreach ($dom->getElementsByTagNameNS("urn:oasis:names:tc:emergency:cap:1.1", "event") as $event) { $events[] = $event->nodeValue; } $alertValues = array('Tornado Warning', 'Severe Thunderstorm Warning', 'Tornado Watch', 'Severe Thunderstorm Watch', 'Flash Flood Warning', 'Flash Flood Watch', 'Flood Warning', 'Flood Watch', 'Flood Advisory', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory', 'Special Weather Statement'); $events = array_intersect($alertValues, $events); // Set the alert colors for the banner background switch(TRUE) { case 'Tornado Warning': $alertColor = 'rgba(255, 0, 0, 0.4)'; break; case 'Severe Thunderstorm Warning': $alertColor = 'rgba(255, 165, 0, 0.4)'; break; case 'Tornado Watch': $alertColor = 'rgba(255, 255, 0, 0.4)'; break; case 'Severe Thunderstorm Watch': $alertColor = 'rgba(219, 112, 147, 0.4)'; break; case 'Flash Flood Warning': $alertColor = 'rgba(139, 0, 0, 0.4)'; break; case 'Flash Flood Watch': $alertColor = 'rgba(46, 139, 87, 0.4)'; break; case 'Flood Warning': $alertColor = 'rgba(0, 255, 0, 0.4)'; break; case 'Flood Watch': $alertColor = 'rgba(46, 139, 87, 0.4)'; break; case 'Flood Advisory': $alertColor = 'rgba(0, 255, 127, 0.4)'; break; case 'Winter Storm Warning': $alertColor = 'rgba(255, 105, 180, 0.4)'; break; case 'Winter Storm Watch': $alertColor = 'rgba(70, 130, 180, 0.4)'; break; case 'Winter Weather Advisory': $alertColor = 'rgba(123, 104, 238, 0.4)'; break; case 'Special Weather Statement': $alertColor = 'rgba(255, 228, 181, 0.4)'; } // Lets assign some styles for the banner $bannerStyle = '"color:' . $alertText . '; width:100%; border-top-style:solid; border-bottom-style:solid; border-color:#FFF; border-width:1px; font-size:12px; text-shadow: 0 1px 1px #111; font-weight:700; font-family: Arial,Helvetica,sans-serif; text-align: center; background-color:' . $alertColor . '; margin-bottom:5px; padding: .2em 0em .2em 0em"'; // Lets assembly the banners to display foreach ($events as $message) { echo '<div style=' . $bannerStyle . '> <strong>WEATHER BROADCAST ALERT - ' . $message . '</strong>... Listen now to our LIVE NOAA Weather Radio Stream <a href=" ' . $feed . '" class="NWRLink" onclick="return popup(this, \'noaa\')" title="Live NOAA Radio For Dallas County">[Click here to listen]</a> </div>'; }
  5. Oh good God, I can't believe I overlooked that. This was a script a friend of mine started over a year ago and I was trying to finish it. Made the correct CSS changes to the below and all is good. style="color:<?php echo $LTaw_departTxt; ?>; background-color:<?php echo $LTaw_departScale; ?>">
  6. Hello, I have a small bit of code that changes the background color of a table cell depending on the value. It works great if I call the script by itself as you can see from this link. http://www.mesquiteweather.net/inc/inc-lake-levels.php But when I include it in on the page where I want to show it like so below, the background colors do not render color correctly as they as suppose to like from the link about, only the text color. It looks like this. I call it in a the table like so. <td style="color:<?php echo $LRH_departTxt; ?>" bgcolor="<?php echo $LRH_departScale; ?>"> <?php echo $LRH_calcDepart; ?></td> I am not sure if this is a PHP problem or a CSS issue but here is the bit of code that creates the colors based on the values. First part is the background color, second part is the text. $LRH_departScale = ''; if ($LRH_calcDepart <= 0 && $LRH_calcDepart > -2) { $LRH_departScale = 'rgb(82, 71, 59)'; } else if ($LRH_calcDepart <= -2 && $LRH_calcDepart > -4) { $LRH_departScale = 'rgb(192, 185, 67)'; } else if ($LRH_calcDepart <= -4 && $LRH_calcDepart > -6) { $LRH_departScale = 'rgb(192, 102, 67)'; } else if ($LRH_calcDepart <= -6 && $LRH_calcDepart > - { $LRH_departScale = 'rgb(185, 64, 76)'; } else if ($LRH_calcDepart <= -8 && $LRH_calcDepart > -10) { $LRH_departScale = 'rgb(170, 59, 145)'; } else if ($LRH_calcDepart <= -10) { $LRH_departScale = 'rgb(115, 58, 167)'; } $LRH_departTxt = ''; if ($LRH_calcDepart <= 0 && $LRH_calcDepart > -2) { $LRH_departTxt = 'rgb(186, 245, 171)'; } else if ($LRH_calcDepart <= -2 && $LRH_calcDepart > -4) { $LRH_departTxt = 'rgb(255, 220, 177)'; } else if ($LRH_calcDepart <= -4 && $LRH_calcDepart > -6) { $LRH_departTxt = 'rgb(255, 219, 177)'; } else if ($LRH_calcDepart <= -6 && $LRH_calcDepart > - { $LRH_departTxt = 'rgb(254, 177, 185)'; } else if ($LRH_calcDepart <= -8 && $LRH_calcDepart > -10) { $LRH_departTxt = 'rgb(245, 170, 228)'; } else if ($LRH_calcDepart <= -10) { $LRH_departTxt = 'rgb(208, 169, 243)'; } Other than the background colors not rendering correctly when included all else works fine. Is there something I am not doing correctly that is causing this? I am not even sure where the first place to start looking would be. -Thanks
  7. I am really not sure what more I could add that isn't already explained and added with the code in the orignal post. The code posted should do what I need it to do but, it doesn't write or read to the cache. That was the reason for this post. When the page is called the script will write to a cache file the response from the API. If the response is under X age then don't call the API. If it is over X age then request a new response and cache it. The script will read from the cache the response. The part of the script that reads from the cache works great. I am just having problems with writing to cache and having the code read from the cache file.
  8. Currently I use a cron to write to a file then the script reads from that file. I am trying to do it without having to use a cron. That is what I was trying to accomplish with the code in my original post. Otherwise it is redundant with the suggested code since wget in a cron does the same thing.
  9. I seem to have this working fairly well now. It writes and reads from it. How would I add to it so it only checks the URL X amount of times I.E. every hour? -Thanks
  10. Thanks for the suggestion. I been trying it out. I do already have the script that will read from a cache file and it works great. This issue is trying to cache the API response from a URL. A couple of questions. Should the below set to true and would the $file be the URL to the API response I want to cache? var $caching = false; var $file = '';
  11. Hello, I am needing to cache a json response from an API so I am not making requests on each load. I have come across some code that is suppose to accomplish this but, it seems to not be working and can't figure out as to why. I created the cache file with 777 but it doesn't write or read from that file when I echo out the results. The code below is just what is suppose to get the contents and cache it. At the end I tried to print it so I can test it to see it prints the response from the cache file but, nothing and the cache file does exists. This is the first time I have tried something like this so please be gentle. // cachePath is name of the path and file used to store cached current conditions gathered from the API request $cachePath = "./cache/nowcast-cache.txt"; // URL to the API request $url = "http://api.wunderground.com/api/XXXXXXXXXXXX/geolookup/conditions/q/TX/mesquite.json"; date_default_timezone_set('America/Chicago'); /** * Request jobs from API * * Split the request into smaller request chunks * and then consolidate them into a single array to limit the API * requests. */ function api_request() { file_put_contents($cachePath, file_get_contents($url)); } /** * API Request Caching * * Use server-side caching to store API request's as JSON at a set * interval, rather than each pageload. * * @arg Argument description and usage info */ function json_cached_api_results( $cache_file = NULL, $expires = NULL ) { global $request_type, $purge_cache, $limit_reached, $request_limit; if( !$cache_file ) $cache_file = dirname(__FILE__) . $cachePath; if( !$expires) $expires = time() - 2*60*60; if( !file_exists($cache_file) ) die("Cache file is missing: $cache_file"); // Check that the file is older than the expire time and that it's not empty if ( filectime($cache_file) < $expires || file_get_contents($cache_file) == '' || $purge_cache && intval($_SESSION['views']) <= $request_limit ) { // File is too old, refresh cache $api_results = api_request(); $json_results = json_encode($api_results); // Remove cache file on error to avoid writing wrong xml if ( $api_results && $json_results ) file_put_contents($cache_file, $json_results); else unlink($cache_file); } else { // Check for the number of purge cache requests to avoid abuse if( intval($_SESSION['views']) >= $request_limit ) $limit_reached = " <span class='error'>Request limit reached ($request_limit). Please try purging the cache later.</span>"; // Fetch cache $json_results = file_get_contents($cache_file); $request_type = 'JSON'; } return json_decode($json_results); } print_r($json_results);
  12. 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. 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;
  13. 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;
  14. 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;
  15. 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.
  16. 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?
  17. 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"; ?>
  18. 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
  19. 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!
  20. Solved, this works. if (empty($nowcast)) { echo $noMessage; } else { echo $nowCast; } Also instead of... $shortTerm = It needed to be this. Notice the . before the equal. That was what was missing the whole time. $shortTerm .=
  21. So basically if the node nowcast = string(0) which means no content I need it to display $noMessage otherwise $shortTerm.
  22. If there is no content for that then the response from the API for that node just shows "nowcast": and that's it. I am back to square one now. All I need to know which I am struggling with after days and days of reading and research for how to do something which I have done many times before with SimpleXML is how to display an alternate message which I already have formatted and just need it to show when the above node response from the API has no content. Can someone PLEASE show me how I can accomplish this?
  23. By value, do you mean if the node has content? Not always will it have content. Hence the reason I want to display an alternative message so there is not just an empty div with there is no content I.E. For example, with content..... "nowcast":"This is a test of the script caching. When this message updates or goes away then all is good. So far all is good since this is currently displaying. It is always sunny in Philideplhia" Without content..... That is how the response shows when it is decoded. So if "nowcast": is empty and there is no content then I want to show an alternative message. This shouldn't be THAT hard or difficult of a question. I have done this numerous times with SimpleXML but, this is the first time I have tried JSON and everything I have tried has failed and every example I have found doesn't work or I am told it is wrong. How am I suppose to know any better. Again, keep in mind. I am just learning, you have to keep it simple and give me examples to work with. I don't see how a var_dump would accomplish that since all it returns is the same thing as an echo would except it has string(184) before the content if there is content and nothing if there isn't.
  24. I just need to check if it is empty, I don't need to know the values. If empty show this, if not then show that. I don't remember it being this difficult with SimpleXML. Could you please give me an example. I will keep that in mind as I like that better and I think it is cleaner. It is like I do for SimpleXML. It was only done that way as that is how the tutorial I was reading was showing how to do it. Keep in mind, I am just learning this.
  25. I am not quite sure what you mean. The variable name in this case is irrelevant though as one is cast and the other is Cast but, for sake of simplicity as it was only for testing I have changed it even though it had no bearing over what I was trying to do. Could you please give me an example as I am not sure what you are talking about. Here are the changes I have made. <?php // Lets get parse the data and create the variables $json_string = file_get_contents("http://api.wunderground.com/api/74a4b7a0d0663fd2/geolookup/conditions/q/TX/Mesquite.json"); $parsed_json = json_decode($json_string); $Updated = $parsed_json->{'current_observation'}->{'observation_time'}; $location = $parsed_json->{'location'}->{'city'}; $nowcast = $parsed_json->{'current_observation'}->{'nowcast'}; // Lets make some style classes for your divs $divContainer = "width:{$divWidth};background-color:{$bkgColor};color:{$txtColor};border:{$border} 1px solid;font-size{$fontSize};"; $divHeader = "width:100%;background-color:{$titleBar};"; $divWrapper = "padding: 5px 5px 5px 5px;"; $pStyle = "color:{$titleTxt};margin:0 0 2px 2px;"; // If there is an available message display it $shortTerm = "<h3 class='title'>NWS NowCast<span style='float:right'> {$Updated}</span></h3>"; $shortTerm = "<hr />"; $shortTerm = "<div class='NWSRound'>"; $shortTerm = "<div style='{$divWrapper}'> {$nowcast}</div>"; $shortTerm = "</div>"; $shortTerm = "<table border='0' cellpadding='0' cellspacing='0' width='100%'><tbody><tr><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td class='shadow-mid' width='100%'><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td></tr></tbody></table>"; // If there is no available message then lets display the following $noMessage = "<h3 class='title'>NWS NowCast<span style='float:right'> {$Updated}</span></h3>"; $noMessage = "<hr />"; $noMessage = "<div class='NWSRound'>"; $noMessage = "<div style='{$divWrapper}'> No valid short term forecast for {$location} currently available at this time</div>"; $noMessage = "</div>"; $noMessage = "<table border='0' cellpadding='0' cellspacing='0' width='100%'><tbody><tr><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td class='shadow-mid' width='100%'><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td></tr></tbody></table>"; if (json != null && json.getString($nowcast) != null){ echo $shortTerm; }else{ echo $noMessage; } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.