Help with If...Elseif...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tweakbird
Forum Newbie
Posts: 4
Joined: Sat Jun 05, 2004 6:53 pm

Help with If...Elseif...

Post by tweakbird »

Hope some of the fellow php scripters can lend me a hand...

I'm playing around with displaying the current homeland security threat level, but I am running into issues. I can pull down the xml file, stip it, and then display the current status, but I want to take what the status is and then add some things to it. But I can't this to work. All I get is just an error message that I put in there incase nothing happened. I am sure I am doing something wrong with the If statements.

Code: Select all

<?php 
$threat = eregi_replace('.*CONDITION="(.*)" />', '1', implode('\n', file("http://www.dhs.gov/dhspublic/getAdvisoryCondition"))); 
$low = "LOW"; 
$guarded = "GUARDED"; 
$elevated = "ELEVATED"; 
$high = "HIGH"; 
$severe = "SEVERE"; 
print $threat; 

if ($threat == $low) { 
    $ccode = "Green (low risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase;    
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode"; 
} elseif ($threat == $guarded) { 
    $ccode = "Blue (general risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase; 
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode"; 
} elseif ($threat == $elevated) { 
    $ccode = "Yellow (significant risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase; 
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode";        
} elseif ($threat == $high) { 
    $ccode = "Orange (high risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase; 
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode"; 
} elseif ($threat == $severe) { 
    $ccode = "Red (severe risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase; 
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode"; 
} else { 
            print "Error getting level from dhs.gov"; 
} 


?>
It will print the current threat (print $threat), but then it will just print out the error message on the last else statement. Its not seeing the statements...

Anybody want to chime in on this?

Cheers,
Tweak :?:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try replacing == with ===

can you show us the EXACT output of $threat?

Code: Select all

<?php

 print "Homeland Security Threat Level is $newthreat; condition is code $ccode"; 

?>
should be

Code: Select all

<?php

 print "Homeland Security Threat Level is $newthreat. Condition is code $ccode"; 

?>
:P
tweakbird
Forum Newbie
Posts: 4
Joined: Sat Jun 05, 2004 6:53 pm

Post by tweakbird »

I tired === and it didn't work, this is what it shows:

ELEVATED Error getting level from dhs.gov

The exact output of $threat is ELEVATED or what ever the current status is (LOW, GUARDED, ELEVATED, HIGH, or SEVERE is what will be returned).

I have a print at the top of the code just to make sure I am hitting the xml file, its being stripped, and shows the status. The second part that is showing "error...blah" is on the last else statement.

I also changed the ; Condition to . Condition....
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

why not try this

Code: Select all

<?php
$threat = eregi_replace('.*CONDITION="(.*)" />', '1', implode('\n', file("http://www.dhs.gov/dhspublic/getAdvisoryCondition"))); 
print $threat; 

if ($threat == "LOW") { 
    $ccode = "Green (low risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase;    
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode"; 
} elseif ($threat == "GUARDED") { 
    $ccode = "Blue (general risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase; 
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode"; 
} elseif ($threat == "ELEVATED") { 
    $ccode = "Yellow (significant risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase; 
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode";        
} elseif ($threat == "HIGH") { 
    $ccode = "Orange (high risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase; 
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode"; 
} elseif ($threat == "SEVERE") { 
    $ccode = "Red (severe risk of terrorist attacks)"; 
    $fixcase = ucwords(strtolower($threat));  //fixes the UPPER case issue 
    $newthreat = $fixcase; 
    print "Homeland Security Threat Level is $newthreat; condition is code $ccode"; 
} else { 
            print "Error getting level from dhs.gov"; 
} 

?>
tweakbird
Forum Newbie
Posts: 4
Joined: Sat Jun 05, 2004 6:53 pm

Post by tweakbird »

I tired that as well before I posted on here for some help..same issue with the above (1st posted code).

Output says ELEVATED Error getting level from dhs.gov .

It see what $threat is, it just won't do the if...endif stuff.. :roll:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try:

Code: Select all

<?php
preg_match('/CONDITION="([A-Z]*)"/', file_get_contents('http://www.dhs.gov/dhspublic/getAdvisoryCondition'), $matches);
$threat = $matches[1];
$threats = array(
    'LOW' => 'Green (low risk of terrorist attacks)',
    'GUARDED' => 'Blue (general risk of terrorist attacks)',
    'ELEVATED' => 'Yellow (significant risk of terrorist attacks)',
    'HIGH' => 'Orange (high risk of terrorist attacks)',
    'SEVERE' => 'Red (severe risk of terrorist attacks)'
);
echo 'Threat: '.$threat.'<br />';
if(isset($threats[$threat])){
    echo 'Homeland Security Threat Level is '.ucwords(strtolower($threats[$threat])).'; con
dition is '.$threats[$threat];
} else {
    echo 'Invalid Threat Code';
}
?>
tweakbird
Forum Newbie
Posts: 4
Joined: Sat Jun 05, 2004 6:53 pm

Post by tweakbird »

sweet.....

I found that my problem was there was white space in the $threat, so I had to add trim() do it and that licked the issue. I was also point to use switch to compare the single vars.

Cheers...
Post Reply