onceinabluemoon2 Posted November 5, 2009 Share Posted November 5, 2009 Hi, I'm getting the temperature value off of the Environment Canada website along with the icon filename, so I can display my own icons and temperature on my site. All this works and I can display the temperature but my conditional statements aren't working to display my icons. I'm new to PHP and I don't see why this is a problem: <?php $url = "http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html"; $ch = curl_init();//Initialise CURL curl_setopt($ch, CURLOPT_URL, $url);//Set the url curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);//We want it to return data curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 100);//Of course, we don't want your script to run forever, so set a timeout $text = curl_exec($ch);//Execute and get the page $exploded = explode('<p class="temperature">',$text);//Make an array of the part before and after the <title> tag $result = explode('<sup>',$exploded[1]);//Grab the part after the <title> tag, and split that too $exploded2 = explode('<img id="currentimg" src="/weathericons/',$text);//Make an array of the part before and after the <title> tag $result2 = explode('.gif"',$exploded2[1]);//Grab the part after the <title> tag, and split that too curl_close($ch);//Close cURL if($result2[0] < "02"){ echo "<img src=\"images/weather/00.gif\" alt=\"Sunny\">"; } if(($result2[0] > "01") && ($result2[0] < "09")) { echo "<img src=\"images/weather/01.gif\" alt=\"Cloudy with chance of showers\">"; } if($result2[0] == "09" || $result2[0] == "19" || $result2[0] == "39") { echo "<img src=\"images/weather/03.gif\" alt=\"Storm\">"; } if(($result2[0] > "9" && $result2[0] < "9") || ($result2[0] > "19" && $result2[0] < "25") || ($result2[0] > "26" && $result2[0] < "30") || ($result2[0] > "31" && $result2[0] < "39")) { echo "<img src=\"images/weather/02.gif\" alt=\"Cloudy with chance of showers\">"; } if(($result2[0] > "24" && $result2[0] < "27") || ($result2[0] > "29" && $result2[0] < "32") || ($result2[0] > "39") { echo ""; } else{ echo ""; } echo $result[0];//Echo the result ?> Quote Link to comment https://forums.phpfreaks.com/topic/180427-solved-quick-fix-conditional-statement-with-an-array/ Share on other sites More sharing options...
abazoskib Posted November 5, 2009 Share Posted November 5, 2009 Are you trying to do number comparison, or string comparison? Quote Link to comment https://forums.phpfreaks.com/topic/180427-solved-quick-fix-conditional-statement-with-an-array/#findComment-951925 Share on other sites More sharing options...
onceinabluemoon2 Posted November 5, 2009 Author Share Posted November 5, 2009 Haha sorry I posted the wrong code. I'm trying to do number comparison. The filename is a two digit number but is taken as a string and I need to convert that string to a number PHP can use. Here is what I meant: <?php $url = "http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html"; $ch = curl_init();//Initialise CURL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 100); $text = curl_exec($ch);//Execute and get the page $exploded = explode('<p class="temperature">',$text); $result = explode('<sup>',$exploded[1]); $exploded2 = explode('<img id="currentimg" src="/weathericons/',$text); $[color=red]result2[/color] = explode('.gif"',$exploded2[1]); curl_close($ch);//Close cURL [color=red]$result2[0] = $num;[/color] if($num < 2){ echo "<img src=\"images/weather/00.gif\" alt=\"Sunny\">"; } if(($num > 1) && ($num < 9)) { echo "<img src=\"images/weather/01.gif\" alt=\"Cloudy with chance of showers\">"; } if($num == 9 || $num == 19 || $num == 39) { echo "<img src=\"images/weather/03.gif\" alt=\"Storm\">"; } if(($num > 9 && $num < 9) || ($num > 19 && $num < 25) || ($num > 26 && $num < 30) || ($num > 31 && $num < 39)) { echo "<img src=\"images/weather/02.gif\" alt=\"Cloudy with chance of showers\">"; } if(($num > 24 && $num < 27) || ($num > 29 && $num < 32) || ($num > 39) { echo ""; } else{ echo ""; } echo $result[0];//Echo the temperature ?> Quote Link to comment https://forums.phpfreaks.com/topic/180427-solved-quick-fix-conditional-statement-with-an-array/#findComment-951973 Share on other sites More sharing options...
abazoskib Posted November 5, 2009 Share Posted November 5, 2009 maybe you meant to do: $num = $result2[0]; ? Quote Link to comment https://forums.phpfreaks.com/topic/180427-solved-quick-fix-conditional-statement-with-an-array/#findComment-951976 Share on other sites More sharing options...
mikesta707 Posted November 5, 2009 Share Posted November 5, 2009 if you want to convert a numeric string into a number you can type cast it to an int like so $number = (int)$stringofNumber; or use the function intval $number = intval($stringofNumber); Quote Link to comment https://forums.phpfreaks.com/topic/180427-solved-quick-fix-conditional-statement-with-an-array/#findComment-951981 Share on other sites More sharing options...
onceinabluemoon2 Posted November 5, 2009 Author Share Posted November 5, 2009 Yes, and I forgot a close bracket and a one on the 9 to make 19. I then had to use: $num = intval($idnum[0]); I got it working now, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/180427-solved-quick-fix-conditional-statement-with-an-array/#findComment-952005 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.