RSS feed with sorting by state and news type but need to display no loop message

Hi everyone. I’m relatively new to PHP and am working on this page http://www.aguivera.com/form.php.
I need to ingest RSS for certain weather warnings and I’ve been able to do it and sort by a specific type of warning. What I’m trying to do now is show a message when there is no data that matches the specific weather warning to appear when someone selects their state.

Currently I do get a message but it keeps looping the amount of times set in my RSS $limit.

This is my code. I know it’s not pretty but I’m more interested in trying to get this to work.
Any guidance or direction would be greatly associated.

my code:

[php]

--Select State-- Alabama Alaska AR <?php $state = new DOMDocument(); if(isset($_POST["state"])){ $state->load($_POST["state"]); $feed = array(); foreach ($state->getElementsByTagName('entry') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('summary')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('id')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('published')->item(0)->nodeValue, 'type' => $node->getElementsByTagName('event')->item(0)->nodeValue, ); array_push($feed, $item);} $limit =10; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $category = $feed[$x]['type']; $date = date('l F d, Y', strtotime($feed[$x]['date'])); if($category == 'Flood Warning') { echo '

'.$title.'
'; echo 'Posted on '.$date.'

'; echo '

'.$description.'

'; echo '

Type of event: '.$category.'

'; } else { echo "no warning"; } } } ?>

[/php]

You are looping regardless of the situation:
[php]
foreach( $feed as $key => $val ){
echo “

”;
print_r( $val );
echo “
”;
}[/php]

How would I change? Would changing that stop the code from listing all messages?

How many messages do you want displayed? You can filter the result set.

There could be any number of messages, so it could be as little as 5 but potentially 15-20. If there is no result for my set filters then a message saying there is nothing should only appear once. Does that make sense?

I can understand that, but why this:
[php]if($category == ‘Flood Warning’)[/php]

Will different types of messages display differently?

That defines the one type of messages I want displayed. I only need flood warning messages since the RSS feed contains all sorts of other weather related warnings.

If a flood warning message doesn’t exist for a specific user selected state then I want a single message that says “there are no warning messages for your state”.

Later this evening I will have modified code. The replacement code I had in length isn’t available now. But you want to move the array_push to within you category check, if you only want one type of alert.

That would be incredible, I’d definitely appreciate it and will donate for your time.

Thank you so much.

R

Testable:

[php]
if ( $_SERVER[‘REQUEST_METHOD’] == ‘POST’){
define(‘DISPLAY_LIMIT’, 5); // this is the variable to change the displayed limit
$count = 0;
$state = new DOMDocument();

$state->load($_POST["state"]);

$feed = array();

foreach ($state->getElementsByTagName('entry') as $node) {
    $item = array(
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('summary')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('id')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('published')->item(0)->nodeValue,
        'type' => $node->getElementsByTagName('event')->item(0)->nodeValue
    );
    if ( strpos($item['title'], 'Flood Warning') != -1) {
        array_push($feed, $item);
        $count++;
        if( $count == DISPLAY_LIMIT){
            break;
        }
    } 
}

foreach ($feed as $key => $val) {
    $title = str_replace(' & ', ' &amp; ', $val['title']);
    $link = $val['link'];
    $description = $val['desc'];
    $category = $val['type'];
    $date = date('l F d, Y', strtotime($val['date']));
    echo "<h2>{$val['title']}</h2>";
    echo "<p><strong><a href='$link' title='$title'>$title</a></strong><br />";
    echo "<small><em>Posted on $date</em></small></p>";
    echo "<p>$description</p>";
}

}

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service