Jump to content

Switch function


Texan78

Recommended Posts

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>';
}

post-150330-0-08814500-1429974839_thumb.png

Link to comment
Share on other sites

  • 3 weeks later...

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. 

 

Screen%20shot%202015-05-13%20at%207.21.5

 

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) {
Link to comment
Share on other sites

I have done a lot of searching of how I can loop through this array and only display a single entry from the array if one of those values exsist in the RSS feed. 

 

How can I do this without using foreeach? Otherwise it works great, I just only need to display one and it is the one with the most priority and the priority is set by the order they are listed in the array. 

 

How can I accomplish this?

 

-Thanks

Link to comment
Share on other sites

If you only want to display the first index in your array, you don't need a loop at all. just print $events[0].

 

I get the feeling there's more to the code that we're not seeing, so it does make it a bit difficult to do anything other than throw something at the wall to see if it sticks...

Link to comment
Share on other sites

the program logic that you are showing us makes no sense. the $event->nodeValue you are testing to come up with a color is the last value from the loop that's building the $events array. unless there's only one event or all the events are the same type, this won't result in the color matching the event type. regardless of your intent to loop or not, the value you test to come up with the color needs to be the $message value you are actually displaying.

 

as to your current question, it's not clear if you want the first event or the first event of each type to be displayed. if you show an example of what the $events array looks like (and is the data from the rss feed in any particular order?) and what result you want based on that data, someone can tell you how to proceed.

 

btw - your big long switch statement can be changed to a single program statement, assuming you switch (pun intended) to using a data driven design. see the following - 

// define the event type/name to color association (this would typically be stored in an included config file or in a database table)
$color_mapper['Tornado Warning'] = 'rgba(255, 0, 0, 0.4)';
$color_mapper['Severe Thunderstorm Warning'] = 'rgba(255, 165, 0, 0.4)';
$color_mapper['Tornado Watch'] = 'rgba(255, 255, 0, 0.4)';
$color_mapper['Severe Thunderstorm Watch'] = 'rgba(219, 112, 147, 0.4)';
$color_mapper['Flash Flood Warning'] = 'rgba(139, 0, 0, 0.4)';
$color_mapper['Flood Warning'] = 'rgba(46, 139, 87, 0.4)';
$color_mapper['Flash Flood Watch'] = 'rgba(0, 255, 0, 0.4)';
$color_mapper['Flood Watch'] = 'rgba(46, 139, 87, 0.4)';
$color_mapper['Flood Advisory'] = 'rgba(0, 255, 127, 0.4)';
$color_mapper['Winter Storm Warning'] = 'rgba(255, 105, 180, 0.4)';
$color_mapper['Winter Storm Watch'] = 'rgba(70, 130, 180, 0.4)';
$color_mapper['Winter Weather Advisory'] = 'rgba(123, 104, 238, 0.4)';
$color_mapper['Special Weather Statement'] = 'rgba(255, 228, 181, 0.4)';

// at the point of mapping the event type/name to the color
$alertColor = isset($color_mapper[$message]) ? $color_mapper[$message] : 'default value goes here...';
Link to comment
Share on other sites

  • 2 weeks later...

The problem isn't the color anymore. I have gotten that sorted. What I can't seem to resolve is for it to only show one single entry from the array and not loop through all of it and display everything it is able to match from the RSS feed it is parsing. I just need to display a single entry by priority which is set in the array. 

 

In case it was missed above, here is the array and this is the order in which a SINGLE entry should display if the rss contains this. At this time, it will display multiple entries if it is contained in the rss feed which I don't want, only a single entry set by it's priority. 

$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');
Link to comment
Share on other sites

YES!, That is exactly what I am needing but, how would that work exactly? How would I set up the in_array()?

 

This is what I have come up with but, it's not working so I know I am doing something wrong. According to the examples I have read this should work. 

 

The in_array() is at the bottom of the code. 

//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';
$event = ' ';

// 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', '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
if (in_array($events, $alertValues)) {
    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>';
}
Edited by Texan78
Link to comment
Share on other sites

Well, after playing with it some more this is what I did and it seems to be working. I am not sure if this is right but, it is working at the moment. 

// Lets assembly the banners to display
if (in_array($event->nodeValue, $alertValues)) {
    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>';
}

Link to comment
Share on other sites

the code you have is not looping over anything. it's also (still) using the $event->nodeValue, which is just the last event that was added to the $events array.

 

example that takes the statement of what to do and produces the code to do it - 

// i would loop over the $alertValues array, checking if each value in turn is in the $events array (see in_array()) and stop on the first match.
$message = ''; // default to an empty string for the result
foreach($alertValues as $value){ // loop over the $alertValues array
    if(in_array($value,$events)){ // checking if each value in turn is in the $events array
        $message = $value; // a match was found, save it as the result
        break; // stop on the first match
    }
}

after the above code runs, $message will either be an empty string or the highest type event found in the $events array.

Edited by mac_gyver
Link to comment
Share on other sites

Would this be correct then for displaying the message per the code you supplied? It works but, just want to make sure the if statement is the correct way. BTW, thank you for your help. 

$message = ''; // default to an empty string for the result
foreach($alertValues as $value){ // loop over the $alertValues array
    if(in_array($value,$events)){ // checking if each value in turn is in the $events array
        $message = $value; // a match was found, save it as the result
        break; // stop on the first match

    }
}

//Display weather radio banner if alert is found
if($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>';
} 

 

it's also (still) using the $event->nodeValue, which is just the last event that was added to the $events array.

 

I am not sure what you were referring to but did you mean the $alertValues array? While it may have been incorrect, it was working and pulling the highest alert at the time which wasn't the last event added. Before when it was looping through all of them it was showing 5-6 different banners at the same time instead of just one, the one being the highest as I wanted. The highest being Severe Thunderstorm Warning and lowest being Flood Warning at that time. Once I used the first code I had put together it worked and only showed one banner as I wanted to and it was Severe Thunderstorm Warning, once that expired it showed the next highest one which was Tornado Watch, then once it expired, it showed the next lowest one which was Flood Warning. So it was working, the logic may have just not been correct. Since I don't know the difference, if it works, I assume it's correct. Which is why I asked if it was correct. I do appreciate your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.