...

View Full Version : Creating A File



Pee-H-Pee
09-30-2004, 06:55 PM
I am trying to create a file for each of my categories and list all the products within the particular categories. What I got so far seems to be working but it keeps generating the header and footer files multiple times, once for each product in the category.

I am sure it is just a looping issue (I totally suck at working with that stuff). My code is below. Any tips on what to change so the header and footer only get produced once would be appreciated.

I need the files to be created like such:

HEADER INFO HERE
product 1
product 2
product 3
product 4
product 5
etc...
FOOTER INFO HERE

An example code would be nice since I am still pretty new at PHP :D


$header = "<!--#INCLUDE VIRTUAL=\"header.html\"-->";
$footer = "<!--#INCLUDE VIRTUAL=\"footer.html\"-->";

$result = mysql_query("select * from products") or die (mysql_error());
while ($row = mysql_fetch_array($result))
{

$catname = ereg_replace(" ", "-",$row[CatName]);
$catpage = "$row[name] - $row[package_type]<br>";
$content = $header . $catpage . $footer;

$fp = fopen("files/$catname.html", 'a');
chmod("files/$catname.html", 0777);
fwrite($fp, $content);
fclose($fp);

}

mysql_free_result($result);

jamescover
10-01-2004, 03:06 AM
I am trying to create a file for each of my categories and list all the products within the particular categories. What I got so far seems to be working but it keeps generating the header and footer files multiple times, once for each product in the category.

I am sure it is just a looping issue (I totally suck at working with that stuff). My code is below. Any tips on what to change so the header and footer only get produced once would be appreciated



Your loop does so as long as a row is returned, and starts here:


while ($row = mysql_fetch_array($result)){

$catname = ereg_replace(" ", "-",$row[CatName]);
$catpage = "$row[name] - $row[package_type]<br>";
$content = $header . $catpage . $footer;

$fp = fopen("files/$catname.html", 'a');
chmod("files/$catname.html", 0777);
fwrite($fp, $content);
fclose($fp);


It ends here:

}



Whatever your trying to output, you'll have to remove your $header and $footer variables from this loop. I don't have time to go through it, but at least this will bump your thread :thumbsup:



-james

Pee-H-Pee
10-01-2004, 09:18 PM
I don't understand this but thanks :)
Can anyone point me in the right direction?


Your loop does so as long as a row is returned, and starts here:


while ($row = mysql_fetch_array($result)){

$catname = ereg_replace(" ", "-",$row[CatName]);
$catpage = "$row[name] - $row[package_type]<br>";
$content = $header . $catpage . $footer;

$fp = fopen("files/$catname.html", 'a');
chmod("files/$catname.html", 0777);
fwrite($fp, $content);
fclose($fp);


It ends here:

}



Whatever your trying to output, you'll have to remove your $header and $footer variables from this loop. I don't have time to go through it, but at least this will bump your thread :thumbsup:



-james

firepages
10-02-2004, 05:02 AM
jamescover was saying that need to realise that every instruction within the while(){} loop is carried out for each row in your database.

Also if you want 1 html file per category then you have to alter your logic .. this is only one of several ways of going about this.

Note that the method used here is ok for small amounts of data , but if you were creating thousands of files then it would be better to move the file operations into the loop itself though this also poses issues.



<?
$header = "<!--#INCLUDE VIRTUAL=\"header.html\"-->";
$footer = "<!--#INCLUDE VIRTUAL=\"footer.html\"-->";
$cats = array();$catname = $catpage = '' ;

$result = mysql_query("SELECT* FROM products") or die (mysql_error());
while ( $row = mysql_fetch_assoc( $result ) ){
$catname = str_replace(" ", "-",$row['CatName']);
$catpage = "{$row['name']} - {$row['package_type']}<br>";
#here we concatenate the $content data into the correct category array#
$cats[$catname] .= $catpage ;
}
?>
we stored the data in an array , so loop through & deal with it
used @chmod as file may not exist for the first chmod
& chmod to something safer after use
<?
foreach( $cats as $catname => $content ){
@chmod("files/$catname.html", 0777);
$fp = fopen("files/$catname.html", 'a+');
fwrite($fp, $header . $content. $footer);
fclose($fp);
chmod("files/$catname.html", 0755);
}
mysql_free_result($result);
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum