Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Creating A File

  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating A File

    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

    PHP Code:
    $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); 

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Pee-H-Pee
    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



    -james
    Last edited by jamescover; 10-01-2004 at 03:10 AM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #3
    New Coder
    Join Date
    Jan 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't understand this but thanks
    Can anyone point me in the right direction?

    Quote Originally Posted by jamescover
    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



    -james

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    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.

    PHP Code:
    <?
    $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);  
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •