msimonds Posted March 18, 2008 Share Posted March 18, 2008 I am trying to write data to an HTML file. I want this to perform only once every 24 hours. I am having issues with the following code: <?php error_reporting(E_ALL & ~E_NOTICE); $html_file = "nba_scores.html"; $cache_reload_seconds = 86400; if (file_exists($html_file)) { if ((filemtime($html_file) + ($cache_reload_seconds - 90)) > time()) { $test = write_to_html($html_file); } else { unlink($html_file); } } function write_to_html($file) { include_once ('class_ScoreScraper.php'); $ss = new ScoreScraper(); $league = 'nba'; $ss->fetchScore($league); foreach ($ss->scores as $score) { if ($score['isStarted']) { $nba_output .= "<tr>\n\t<td class='alt1'><div class='smallfont'><strong>{$score['homeTeam']}</strong></div></td>\n\t<td align='right' class='alt1'><div class='smallfont' align='right'><strong>{$score['homeTeamScore']}</strong></div></td>\n</tr>\n"; $nba_output .= "<tr>\n<td class='alt1'><div class='smallfont'><strong>{$score['awayTeam']}</strong></div></td>\n\t<td align='right' class='alt1'><div class='smallfont' align='right'><strong>{$score['awayTeamScore']}</strong></div></td>\n</tr>\n"; $nba_output .= "<tr>\n<td class='alt1'> </td><td align='right' class='alt1'> </td>\n</tr>\n"; } else { $nba_output .= "<tr>\n\t<td class='alt1'><div class='smallfont'><strong>{$score['homeTeam']}</strong></div></td>\n\t<td align='right' class='alt1'><div class='smallfont' align='right'><strong>Game Time</strong></div></td>\n</tr>\n"; $nba_output .= "<tr>\n\t<td class='alt1'><div class='smallfont'><strong>{$score['awayTeam']}</strong></div></td>\n\t<td align='right' class='alt1'><div class='smallfont' align='right'><strong>{$score['gameTime']}</strong></div></td>\n</tr>\n"; $nba_output .= "<tr>\n<td class='alt1'> </td><td align='right' class='alt1'> </td>\n</tr>\n"; } } file_put_contents($file,$nba_output); return $file; } ?> Problem is that if the file is over 24 hours, the script runs and then deletes the file and then never recreates it because of my stupid code Can someone please help me out TIA, Mike Quote Link to comment https://forums.phpfreaks.com/topic/96702-help-with-writing-to-file/ Share on other sites More sharing options...
laffin Posted March 18, 2008 Share Posted March 18, 2008 I think the logic shud be if (!(file_exists($html_file) && ((filemtime($html_file) + ($cache_reload_seconds - 90)) < time()) )) { if(file_exists($html_file)) unlink($html_file); $test = write_to_html($html_file); } file exists and time less than expiry (which in case we dun want to do anything) which wud be a valid, hey we got our cache and it hasnt expired but the ! in front of the conditionals make it everything else Quote Link to comment https://forums.phpfreaks.com/topic/96702-help-with-writing-to-file/#findComment-494873 Share on other sites More sharing options...
msimonds Posted March 18, 2008 Author Share Posted March 18, 2008 Thanks for that tip, but that doesn't work. That writes to the file every time Quote Link to comment https://forums.phpfreaks.com/topic/96702-help-with-writing-to-file/#findComment-494936 Share on other sites More sharing options...
msimonds Posted March 18, 2008 Author Share Posted March 18, 2008 anyone?? Quote Link to comment https://forums.phpfreaks.com/topic/96702-help-with-writing-to-file/#findComment-495049 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.