Jump to content
Sign in to follow this  
Texan78

Total number from XML values

Recommended Posts

Hello, 

 

I am bit stuck and could use some assistance. I am know I need to probably use an array to filter out only the results I need but, the solutions I have tried have not been successful. 

 

What I am trying to do is get the total number of certain events within an XML file. I have got it to where it prints out all the events within the XML file but, this is where I am stuck. How to filter out only certain events that I want the value of then get a single total combined number of those chosen events. 

 

Here is the test link of what the output looks like now. 

 

http://stream.dfwstormforce.com/inc/alertsCount.php

ini_set('display_errors','1');

## Start Configurable data ##

$data3 = "../xml/nat_warnings.xml";

## End Configurable data ##

// 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($data3);

$events = [];
foreach ($dom->getElementsByTagNameNS("urn:oasis:names:tc:emergency:cap:1.1", "event") as $event) {
    $events[] = $event->nodeValue;
}

//$message = $event->nodeValue;

//$alertValues = array('Tornado Warning', 'Severe Thunderstorm Warning', 'Flash Flood Warning', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory');


    $counts = array_count_values($events);//lets count the results
    //$value = isset($counts['Tornado Warning', 'Severe Thunderstorm Warning', 'Flash Flood Warning', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory')]);

print_r($counts);

-Thanks

Share this post


Link to post
Share on other sites

SimpleXML is so much better for this than DOM.

$sri = htmlspecialchars(/* whatever value you want to count */, ENT_QUOTES);

$xml = new SimpleXMLElement("../xml/nat_warnings.xml", 0, true);
$xml->registerXPathNamespace("cap", "urn:oasis:names:tc:emergency:cap:1.1");

$count = count($xml->query("/feed/entry[cap:event = '{$sri}']"));

Share this post


Link to post
Share on other sites

Hello, 

 

When I try what you suggested which is this. 

ini_set('display_errors','1');

## Start Configurable data ##

##$data3 = "../xml/nat_warnings.xml";

## End Configurable data ##

$sri = htmlspecialchars('Tornado Warning', 'Severe Thunderstorm Warning', 'Flash Flood Warning', ENT_QUOTES);

$xml = new SimpleXMLElement("../xml/nat_warnings.xml", 0, true);
$xml->registerXPathNamespace("cap", "urn:oasis:names:tc:emergency:cap:1.1");

$count = count($xml->query("/feed/entry[cap:event = '{$sri}']"));

print_r($count);

Then I get this error. 

 

 

 

Warning: htmlspecialchars() expects parameter 2 to be long, string given in /home2/mesquiu0/public_html/stream/inc/alertsCountTest.php on line 23

Fatal error: Call to undefined method SimpleXMLElement::query() in /home2/mesquiu0/public_html/stream/inc/alertsCountTest.php on line 28

 

I much prefer SimpleXML but, there is a reason I am using DOM for this as there is something else I will be doing with this later once I get this sorted out. 

Share this post


Link to post
Share on other sites

Please try to understand code before mixing it into what you have.

 

I thought you only wanted one or two values. If you want more than that then you might as well count everything. Which is what your original code, though not necessarily the best approach, was already doing.

 

So is your problem instead that

//$value = isset($counts['Tornado Warning', 'Severe Thunderstorm Warning', 'Flash Flood Warning', 'Winter Storm Warning', 'Winter Storm Watch', 'Winter Weather Advisory')]);
you don't understand PHP? Because

Array
(
    [Special Weather Statement] => 20
    [Tornado Warning] => 1
    [Wind Advisory] => 20
    [Rip Current Statement] => 8
    [Flood Warning] => 480
    [Lake Wind Advisory] => 13
    [Flood Advisory] => 37
    [Flash Flood Warning] => 5
    [Air Quality Alert] => 2
    [High Surf Advisory] => 1
    [Flash Flood Watch] => 3
    [Winter Storm Warning] => 3
    [Flood Watch] => 5
    [Hydrologic Outlook] => 3
    [Winter Weather Advisory] => 11
    [Beach Hazards Statement] => 2
    [Fire Weather Watch] => 2
    [Lakeshore Flood Warning] => 1
    [Severe Thunderstorm Warning] => 1
    [Freeze Warning] => 2
    [Coastal Flood Advisory] => 1
    [Dense Fog Advisory] => 1
)
looks pretty darn close to what you want.

 

You can only look at one value at a time.

$value = isset($counts['Tornado Warning']);
That will only tell you whether there were any tornado warnings at all. Which is a good first step. Once you've determined that there were values you can get the actual count by doing the same thing but without the isset().

Share this post


Link to post
Share on other sites

Ok let me try to explain what I am trying to do. The arrary you see is all the active alerts and how many of that alert is currently active. from the "../xml/nat_warnings.xml" There is a possibility of over 116+ events. This is why I created the arrary to only check the ones I want. In this case I just need to total number of alerts for "'Tornado Warning', 'Severe Thunderstorm Warning', 'Flash Flood Warning'" with the ability to add more as needed depending on the season. With the orginal code that filters what I want and gives me to total number of each alert. So what I am needing now is to take all those numbers and add them together in to one single value which I can call with a single variable. Reason for this is I am making a badge kind of like what you see on iPhones that gives the total number of alerts. So it will take all the alerts I want and add them all together into one single value. 

 

This is where I am stuck is just creating a single variable that will give me the total number of the select alerts I want. 

 

-Thanks

Share this post


Link to post
Share on other sites

So what I am needing now is to take all those numbers and add them together in to one single value which I can call with a single variable.

Then do that.

$value = @$counts["Tornado Warning"] + @$counts["Severe Thunderstorm Warning"] + @$counts["Flash Flood Warning"];
I'm using @ here because PHP will warn if the warning doesn't exist in the array and its default behavior of evaluating to null is acceptable.
  • Like 1

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

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