Hi,
I have a script that creates a HTML for every row in a DB query result. I had this working normally but for some reason I am now having strange problems.
Firstly, there are 2 files: a generator.php file that runs a loop which in turn calls another PHP file and saves the output of that as HTML file with a special naming format.
the second file is the resultscache.php file. This file could be called on its own for any set of results but I need to create HTML from it as a form of caching system.
Below is the generator file (apologies for the length)
<?php
include("dbconnect.php");
$months = array(
array("name"=>"December", "num"=>"12", "num_of_days"=>"31")
);
$years = array(2002);
foreach($years as $year)
{
foreach($months as $month)
{
for($i=30; $i<=$month['num_of_days']; $i++)
{
$startdate = date ("dmY", mktime (0,0,0,$month['num'],$i,$year));
$startyear = date ("Y", mktime (0,0,0,$month['num'],$i,$year));
$startmonth = date ("m", mktime (0,0,0,$month['num'],$i,$year));
$startday = date ("d", mktime (0,0,0,$month['num'],$i,$year));
$srcurl = "http://localhost:9000/horseracingresults/rebuild/resultscache.php";
$query = "SELECT DISTINCT Track, Stdate FROM mkt_result WHERE Stdate = '$startdate'";
$result = ifx_query($query, $conn_id);
//give $track the same value as whatever corresponds with $row[track] at this iteration of the while loop
while($row = ifx_fetch_row($result)){
$track = $row[track];
//check to see if date directory already exists before we try to create it
if (!file_exists("html/".$startyear)) {
//make date directory
mkdir ("html/".$startyear, 0777);
}
if (!file_exists("html/".$startyear."/".$startmonth)) {
//make date directory
mkdir ("html/".$startyear."/".$startmonth, 0777);
}
if (!file_exists("html/".$startyear."/".$startmonth."/".$startday)) {
//make date directory
mkdir ("html/".$startyear."/".$startmonth."/".$startday, 0777);
}
//fix track for HTML name
$track = ereg_replace(" ", "_", trim($track));
$tempfilename = "tempindex.html";
$targetfilename = "html/".$startyear."/".$startmonth."/".$startday."/".$track.$startdate.".html";
//put space back in track for DB query
$track = ereg_replace("_", " ", $track);
?>
<html>
<head>
<title>Generating HTML...</title>
</head>
<body>
<p>Generating <?= $targetfilename?>...</p>
<?php
//change the txt file into a PHP file that replaces placeholder text
with the correct PHP generated Track name (generated from the query)
$tempfile = "resultscache.php";
$fp1 = @fopen($tempfile, 'w');
$fp = @fopen("http://localhost:9000/horseracingresults/rebuild/resultscache.txt", "w")
or die("Couldn't open file");
$data = fread($fp, filesize($fp));
$data .= fread($fp, 100000);
$toreplace = "-track-";
$replacewith = $track;
$data = ereg_replace($toreplace, $replacewith, $data);
$toreplace1 = "-date-";
$replacewith1 = $startdate;
$data = ereg_replace($toreplace1, $replacewith1, $data);
$ok = fwrite($fp1, $data);
fclose($fp);
//generate php file end
@unlink($tempfilename);
$dynpage = fopen($srcurl, 'r');
if(!$dynpage){
echo("<p>Unable to load $srcurl. Static page "."update aborted!</p>");
exit();
}
$htmldata = fread($dynpage, 1024*1024);
fclose($dynpage);
$tempfile = fopen($tempfilename, 'w');
if(!$tempfile){
echo ("<p>Unable to open temporary file");
exit();
}
fwrite($tempfile, $htmldata);
fclose($tempfile);
$ok = copy($tempfilename, $targetfilename);
unlink($tempfilename);
//FTP the file
// set up basic connection
$conn_id = ftp_connect("10.100.58.78");
$track = ereg_replace(" ","_",$track);
// login with username and password
$login_result = ftp_login($conn_id, pictures, paddy);
$source_file
= "C:/Program Files/nusphere/apache/nsdocs/horseracingresults/rebuild/html/"
.$startyear."/".$startmonth."/".$startday."/".$track.$startdate.".html";
//make date directory
@ftp_mkdir
($conn_id,"/usr/local/apache/htdocs/hr/html/".$startyear);
@ftp_mkdir
($conn_id,"/usr/local/apache/htdocs/hr/html/".$startyear."/".$startmonth);
@ftp_mkdir
($conn_id,"/usr/local/apache/htdocs/hr/html/".$startyear."/".$startmonth."/".$startday);
echo $track;
$upload = ftp_put
($conn_id, "/usr/local/apache/htdocs/hr/html/"
.$startyear."/".$startmonth."/".$startday."/".$track.$startdate.".html", $source_file, FTP_ASCII);
}//end db result loop
}
}
}//end dates array foreach
?>
</body>
</html>
Basically, this file first sets some date variables Then it runs through the array using foreach. next it decides what directories to create or if they exist already, ignore them.
Next it opens a resultscache.txt file. It replaces the words -track- and -date- with the date variables set at the top of the script. It then saves this with the replaced -track- etc. as resultscache.php and runs this PHP file. It then saves the output of the PHP file as a HTML file in a certain name format ie: TRACKNAME12012002.html
The contents of the resultscache.php file are fine, but the problem seems to lie in the code above.
It gives an error in the first HTML file generated and it creates every subsequent file from the foreach loop without any problems. The first HTML file is generated with the correct name but when I open that file it has a parse error. There is no parse errors in that code as i checked the file on its own and also, the other HTML files are generated from the same page and they are fine.
I am totally stuck with this so if anyone can help you will be a lifesaver!
Thanks,
Martin