Page 1 of 1

code adjustment to time update

Posted: Wed Apr 18, 2007 8:57 am
by jpowell79
Hi everyone! :)


this is my first posting and I'd like to say thanks in advance to anyone who can help me out here :-)

I have two sets of code which need to update once a day (updates cols.php) at around 08:00am GMT.

I'm not sure how to implement this.....If anyone could help me with a change to the code I'd be most grateful :-)

Code: Select all

<?php
include("cols.php");
if(date("m",$last_updated)!=date("m")||date("d",$last_updated)!=date("d")||date("Y",$last_updated)!=date("Y")){
	$content=file_get_contents("http://horses.sportinglife.com/Naps_Table");
	$needle="<th class=\"napHdr\" style=\"padding-left:0px;\">Selection";
	$start=strpos($content,$needle);
	if($start>0){
		$needle="<tr>";
		$start=strpos($content,$needle,$start)+strlen($needle);
		$needle="</table>";
		$end=strpos($content,$needle,$start);
		$content=substr($content,$start,$end-$start);
		$rows=explode("<tr>",$content);
		$needle=array('<span style="font-weight:bold">','</span>','<a','>','</a>','<div style="font-weight:bold">','</div>','</td>');
		unset($col1,$col2,$col3,$col4);
		for($i=0,$n=count($rows);$i<$n;$i++){
			$offset=0;
			$content=$rows[$i];
			$offset=$start=strpos($content,$needle[0],$offset)+strlen($needle[0]);
			$offset=$end=strpos($content,$needle[1],$offset);
			$lenght=$end-$start;
			$col2[$i]=substr($content,$start,$lenght);
			$offset=$start=strpos($content,$needle[2],$offset)+strlen($needle[2]);
			$offset=$start=strpos($content,$needle[3],$offset)+strlen($needle[3]);
			$offset=$end=strpos($content,$needle[4],$offset);
			$lenght=$end-$start;
			$col1[$i]=substr($content,$start,$lenght);
			$offset=$start=strpos($content,$needle[5],$offset)+strlen($needle[5]);
			$offset=$end=strpos($content,$needle[6],$offset);
			$lenght=$end-$start;
			$col3[$i]=substr($content,$start,$lenght);
			$start=$end+strlen($needle[6]);
			$offset=$end=strpos($content,$needle[7],$offset);
			$lenght=$end-$start;		
			$col4[$i]=substr($content,$start,$lenght);
		}
$last_updated=time();		$file="<?php
		\$last_updated=".time().";\n";
		for($i=0;$i<$n;$i++){
			$file.="\$col1[".$i."]=\"".$col1[$i]."\";\n";
			$file.="\$col2[".$i."]=\"".$col2[$i]."\";\n";
			$file.="\$col3[".$i."]=\"".$col3[$i]."\";\n";
			$file.="\$col4[".$i."]=\"".$col4[$i]."\";\n";
		}
		$file.="?>";
		$fp=fopen("cols.php","w+");
		fwrite($fp,$file,strlen($file));
		fclose($fp);
	}else{
		$n=count($col1);
	}
}else{
	$n=count($col1);
}
?>


thanks

Posted: Wed Apr 18, 2007 9:38 am
by blackbeard
Why don't you use a cron job to run the script once a day?

Posted: Wed Apr 18, 2007 10:22 am
by jpowell79
hi blackbeard,

unfortunately my site is hosted with fasthosts.co.uk who do not support cron jobs :(

Posted: Wed Apr 18, 2007 10:44 am
by RobertGonzalez
If you cannot use cron you are more than likely going to need to trigger the action based on a page load timing and a database tracking mechanism. Basically set up a table that holds the times of the day that each process needs to run. Every page load run a script that checks the current time against when the trigger is supposed to fire. Once you hit that time, fire. Then reset the trigger requirement in the database somehow so that it will run again at the next interval.

This is a very convoluted way to do it. I would ask my host if there were other scheduled task application they support so you can use those instead of a PHP script that requires user interaction.

Posted: Wed Apr 18, 2007 10:50 am
by bert4
Hi,

I have a site where there are plenty of visitors to trigger "jobs", either on the site itself, or on other sites....

This is one to update xml feeds every hour:

Code: Select all

$query = "SELECT * FROM `news_update` WHERE ID = 1";
$result=mysql_db_query ($dbname, $query, $link);
$tRow = mysql_fetch_array ($result) ;
$ddd = date("dH");
if ($tRow[news1_time] <> $ddd) {
 $query = "UPDATE news_update SET news1_time = '$ddd' WHERE ID = 1" ; 
 $result=mysql_db_query ($dbname, $query, $link);
 require ('xmlnewsparser1.php');
}
It just checks if an hour has passed, if so, store the hour of update, and get the xml feeds.

You would need to change the time checking a bit.... and my xmlparser would be your update script....

If it doesn't take to long, nobody will notice :)

regards,

Bert

p.s. I have a check for this bit of code, so it is not triggered by bots.