mjb982 Posted September 22, 2009 Share Posted September 22, 2009 I'm writing a code that pulls current weather conditions from the NWS xml data. The problem I'm running into is I would like custom weather icons, not the default NWS ones. I can accomplish this by using individual if statements for each weather description, ie: if($current['weather']=="Partly Cloudy"){ $icon = "images/pcloudy.png"; } But there are hundreds of them, ie "A Few clouds", "a few clouds and windy" etc... I was wondering if there was a way that I'm not familiar with do make an if statement where if "current['weather']" contiains the words "cloud" then I could display the correct image. That would cut down the time it takes, and make things so much simpler. Something like if($current['weather'] contains "cloud"){ $icon = "images/clouds.png"' } is it possible? Link to comment https://forums.phpfreaks.com/topic/175146-question-on-if-statement/ Share on other sites More sharing options...
knsito Posted September 22, 2009 Share Posted September 22, 2009 I would use str_pos() which will tell you if your string is part of the xml data http://us.php.net/manual/en/function.strpos.php <?php $pos = str_pos('cloud', $current['weather']); if($pos !== false){ $icon = "images/clouds.png"; } ?> Personally I dont like to use ==, !=, etc on strings but stick to the string comparision functions available (see strcmp() etc) Link to comment https://forums.phpfreaks.com/topic/175146-question-on-if-statement/#findComment-923090 Share on other sites More sharing options...
kickstart Posted September 22, 2009 Share Posted September 22, 2009 Hi Another possibility would be to have an associative array of icons keyed on the possible descriptions. All the best Keith Link to comment https://forums.phpfreaks.com/topic/175146-question-on-if-statement/#findComment-923097 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.