Jump to content

Recommended Posts

Now that I have one problem sorted I have a new one. This is kind of like a sort but with a sort it will display then entire array in a certain order. I don't want to display the entire array just a certain value by it's set priority. 

 

I have a variable which is created from parsing an XML file. This variable has 120+ possible values since it is XML and constantly changing. I only want to display 9 of those possible values which I have those singled out now. I only want to show one of them and to determine which one I show I want to put them in a certain order by priority and display the highest priority one from the array. 

 

I don't know how to go about this since sort will display then entire array just in the order you place it. I really can't find any examples of what I am needing to do. 

 

This is the order I am needing them to display in if they exist and how it breaks down. 

 

$event pulls the possible titles from the XML file like so. This works great, no problems here at all. 

 //Set initial output to false
$tData = false;
$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;
Now the only problem I should say with the above is if the $event variable contains more than one of the values I want to display in the list it will display the first one from the XML. So I need to be able to prioritize the values below and sort through that and only show the highest priority value should more than one exist   I hope this makes sense. 
$alertvalue = array('Tornado Warning', 'Severe Thunderstorm Warning', 'Tornado Watch', 'Severe Thunderstorm Watch', 'Flash Flood Warning', 'Flash Flood Watch', 'Flood Warning', 'Flood Watch', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory', 'Special Weather Statement');

If this was a normal sort it would be easy but I don't want to print the entire array just the highest priority one from the array. 

Working again with:

array_intersect($alertValue, $events);

 

$alertValue contains the user-prioritized events: for example, 'Tornado Warning' is the highest priority.

 

The elements in $events will keep the matching elements in $alertValue, that is:

$intersect = array_intersect( array('Tornado Warning','Severe Thunderstorm Warning','Tornado Watch'), array('Tornado Watch','Severe Thunderstorm Warning') );

$intersect will have an array of:

[1] => 'Severe Thunderstorm Warning'

[2] => 'Tornado Watch'

because those were the only matching elements -- and that is the order of $alertValue.

 

Then you array_shift($intersect) to get the highest priority event according to your user-prioritized $alertValue array.

The elements in $events will keep the matching elements in $alertValue, that is:

$intersect = array_intersect( array('Tornado Warning','Severe Thunderstorm Warning','Tornado Watch'), array('Tornado Watch','Severe Thunderstorm Warning') );

 

How did you arrive at that. Where did $events and $intersect come from? If I understand this correctly it will print then entire array within the prioritized order. That is like sort but, it dosen't explain how to print only the highest priority event. I can do all this with sort but it's going to display the entire list in a certain order if those events exist and not just the single highest one. 

Edited by Texan78

There has got to be an easier way to get set the priority and it if a value with a high priority matches a value for the XML variable then display it. 

 

Unless I am doing something wrong which is highly likely then it doesn't work but it seems there is an easier way. 

$titles = array($event);

$alertValue = array('Tornado Warning', 'Severe Thunderstorm Warning', 'Tornado Watch', 'Severe Thunderstorm Watch', 'Flash Flood Warning', 'Flash Flood Watch', 'Flood Warning', 'Flood Watch', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory', 'Special Weather Statement');

$events = array_intersect($alertValue, $titles);

$message = array_shift($events);

print_r($message);

It still outputs the same low priority value. It should be showing Severe Thunderstorm Warning because that is a higher priority which is set as in $alertValue.

 

http://www.mesquiteweather.net/wxtest.php

 

Here is a list of active events which there are some that have higher priority. 

 

http://alerts.weather.gov/cap/tx.php?x=1

 

I don't need it to show all up them, just one and just the highest one by the priority which I set. 

Edited by Texan78

Ok, I think I have found my issue with the below code that parses the XML file. 

//Set initial output to false
$tData = false;
$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;

Now the variable $event is what pulls the alert titles. Through more trouble shooting I have found out that $event is only returning one value when it should be returning more and it is only pulling the last value in the list. So it only has one value to compare against. I guess I need to figure out how to pull all the values for the variable to put into an array.

 

When I put this inside the loop I am able to pull all the values for the variable but then I am stuck again. 

$titles = array($event);

        $alertValue = array('Tornado Warning', 'Severe Thunderstorm Warning', 'Tornado Watch', 'Severe Thunderstorm Watch', 'Flash Flood Warning', 'Flash Flood Watch', 'Flood Warning', 'Flood Watch', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory', 'Special Weather Statement');

        $events = array_intersect($alertValue, $titles);

        $message = array_shift($events);

        echo $message;

Any suggestions?

Edited by Texan78

This statement:

$result = $entries->xpath("//prefix:entry");

seems to be pulling the values of all targeted nodes into the variable $result. Let's use $result assuming it holds all the alerts from the parsed XML.

 

Edit: On second look, maybe $result does not hold the alert phrases. I see that you are still iterating through $results to get at a deeper node of the XML.

 

The result of array_intersect($alertValue, $result) needs to be assigned to a variable. So, for example:

$intersect = array_intersect($alertValue, $result);

 

$intersect is an array that will contain only those elements from $alertValue that were also found in $result, in the same order that $alertValue has them in.

 

Finally,

$message = array_shift($intersect);

to get the first element (which is the highest priority).

Edited by bsmither

So lets look at the loop.

foreach ($result as $entry):
  $event = $entry->children("cap", true)->event;
endforeach;

 

I would like to build $event as an array holding each value of the 'event' XML node. So;

  $event[] = $entry->children("cap", true)->event;

This change will append a new element to the $event array containing the phrase extracted for this iteration of the loop.

 

Then,

$intersect = array_intersect($alertValue, $event);

Ok I think I got this sorted out and working now. Does this look right?

//Set initial output to false
$tData = false;
$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;

$alertValue = 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($alertValue, $event);

        $message = array_shift($events);


// Set the alert colors for the banner background

include ('inc-NWR-alert-colors.php');

// Lets assign some styles for the banner

   $bannerStyle = '"color: #FFF; width:100%; border-top-style:solid; border-bottom-style:solid; border-color:#FFF; border-width:1px; font-size:11px; font-weight:normal; font-family: Arial,Helvetica,sans-serif; text-align: center; background-color:' .$alertColor. '; margin-bottom:5px; padding: .2em 0em .2em 0em"';

// Lets assembly the banner to display

   $radiowarning = '
		<div style='.$bannerStyle.'>
			<strong>SEVERE WEATHER ALERT - '.$message.'</strong>... Listen now to our LIVE NOAA Weather Radio Stream <a href="'.$feed.'" onclick="return popup(this, "noaa")" title="Live NOAA Radio For Dallas County">[Click here to listen]</a>
		</div>';

		foreach( $alertValue as $alert )
	{
		if (in_array($alert, $event))
		{
			echo $radiowarning;
		}
	}

The only small issue I am running into is since there 3 instances of the same alert current at the time it is showing it 3 times. How would I go about only showing it once? This won't be a problem when I am using it on the county feed but state feed it could cause problems as I am seeing. 

 

http://www.mesquiteweather.net/wxtest.php

We have placed the highest priority alert in $message. $message is the only variable that needs to be used in the remainder of the code.

 

The remainder of the code builds $bannerStyle, and $radiowarning incorporates $bannerStyle and $message.

 

So, echo $radiowarning just once.

That's what I did if you notice from the code above. 

 

The issue I am seeing is since we are now pulling every value from $event. If the event is issued more than once then it will display more than once. 

 

Not a big deal if I am using only the county feed as it can only have one of the same event issued at a time but, on a state level there could be multiple events of the same priority. 

What I notice in the code posted in #8, is:

foreach ( $alertValue as $alert ) {
  if ( in_array($alert, $event) ) {
    echo $radiowarning;
  }
}

This is a loop (13 iterations), and depending on the if/then expression, $radiowarning will be echoed -- as many as zero times to as many as 13 times.

Edited by bsmither
  • 3 weeks later...

Revisiting this since I am able to test it with a live feed now. The feed only shows one entry but yet it is being duplicated twice and I am unsure what is causing this. The other day it would repeat 3 or 4 times. I am not sure what is causing this. Any suggestions?

 

http://www.mesquiteweather.net/inc-weather_radio_alert.php

//Set initial output to false
$tData = false;
$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;

        $alertValue = 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($alertValue, $event);

        $message = array_shift($events);


// Set the alert colors for the banner background

include ('inc-NWR-alert-colors.php');

// Lets assign some styles for the banner

         $bannerStyle = '"color: #FFF; width:100%; border-top-style:solid; border-bottom-style:solid; border-color:#FFF; border-width:1px; font-size:11px; font-weight:normal; font-family: Arial,Helvetica,sans-serif; text-align: center; background-color:' .$alertColor. '; margin-bottom:5px; padding: .2em 0em .2em 0em"';

// Lets assembly the banner to display

         $radiowarning = '<div style='.$bannerStyle.'>
			                  <strong>SEVERE WEATHER ALERT - '.$message.'</strong>... Listen now to our LIVE NOAA Weather Radio Stream <a href="'.$feed.'" onclick="return popup(this, \'noaa\')" title="Live NOAA Radio For Dallas County"><span style="color:FFF">[Click here to listen]</span></a>
		                  </div>';

		foreach( $alertValue as $alert )
	{
		if (in_array($alert, $event))
		{
             echo $radiowarning;
		}
	}
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.