Jump to content

Help with writing to file


msimonds

Recommended Posts

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

Link to comment
Share on other sites

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 :)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.