Jump to content

onceinabluemoon2

New Members
  • Posts

    5
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

onceinabluemoon2's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Oh wow... sorry for wasting your guys' time. I'm sure you all had a little chuckle at the ifelse without a condition. Yes, thats what fixed it. Along with just putting the code from weather.php right in the index. mjdamato, thanks for the enlightening perspective, but I'll keep the log file... It works now and thats all I need. PFMaBiSmAd, I tried that line of code in your signature while I still had the error but nothing changed. I don't have access to the php.ini file. I'm not hosting this myself. I guess my host disabled it? It's free so I can't complain. Thanks again both of you.
  2. Hi, I understand that you can use cron jobs to achieve this, but I can't use cron in my case. I have a script that will copy weather details off of the Government of Canada site and put it on my site. If the site gets popular, I don't want the script to fetch the info every time the page refreshes. So far, I have this script put together, however all I can say is that it "doesn't work" I only get a blank page with no errors from my host. The index will see if the script has been ran before 2000 seconds, if not it will execute weather.php, if it has, it will show the buffered results from buffer.log. When weather.php executes, it echos the result to the page and also writes the html to buffer.log This is in index.php <?php $lastRunLog = '/lastrun.log'; $lastRunBuf = '/buffer.log'; if (file_exists($lastRunLog) && file_exists($lastRunBuf)) { $lastRun = file_get_contents($lastRunLog); if (time() - $lastRun >= 2000) { $cron = file_get_contents('weather.php'); file_put_contents($lastRunLog, time()); eval($cron); } elseif { $buffer = file_get_contents($lastRunBuf); echo "$buffer"; } } ?> weather.php <?php $url = "http://www.weatheroffice.gc.ca/city/pages/on-69_metric_e.html"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 100); $text = curl_exec($ch); $buffer = "buffer.log"; $fh = fopen($buffer, 'w') or die("Can not open buffer"); $exploded = explode('<p class="temperature">',$text); $result = explode('<sup>',$exploded[1]); $exploded2 = explode('<img id="currentimg" src="/weathericons/',$text); $idnum = explode('.gif"',$exploded2[1]); if ($idnum[0] == ''){ $idnum[0] = '50'; } $exploded3 = explode('<dt>Condition:</dt>',$text); $iden = explode('</dd>',$exploded3[1]); curl_close($ch); $iden2 = ereg_replace("<dd>", "", $iden[0]); $iden3 = ereg_replace(" ", "", $iden2); $num = intval($idnum[0]); if($num < 10){ $stringdata1 = "<img src=\"images/weather/0$num.png\" style=\"border-style: none\">"; echo "$stringdata1"; fwrite($fh, $stringdata1); } if($num > 9 && $num < 50){ $stringdata2 = "<img src=\"images/weather/$num.png\" style=\"border-style: none\">"; echo "$stringdata2"; fwrite($fh, $stringdata2); } if($num == 50){ $stringdata3 = "<img src=\"images/weather/31.png\" style=\"border-style: none\">"; echo "$stringdata3"; fwrite($fh, $stringdata3); } else{ $stringdata4 = " "; echo "$stringdata4"; fwrite($fh, $stringdata4); } $stringdata5 = " <span id=\"number\">$result[0]</span> $iden3"; echo "$stringdata5"; fwrite($fh, $stringdata5); fclose($fh); ?> buffer.log and lastrun.log exist and is blank. Thanks for reading. Hopefully its a stupid mistake, I'm still pretty new to PHP. My apologies for the bad spacing, some formatting issues with Dreamweaver.
  3. 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!
  4. 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 ?>
  5. 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 ?>
×
×
  • 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.