1

We recently built a PHP e-commerce website for a client selling children's toys, since launch he has brought in a promotion agency who have recommended having static HTML pages for each of his products. He has over 2,5000 products so manually building each one is not a realistic option.

Is it possible with PHP to read each product from the database and create a new file based on the product name and populate templated areas with descriptions, images and links? If so has anyone any suggestions as to where I can start?

2
  • I know this doesn't answer your question, but you should highly consider a solution like Varnish (en.wikipedia.org/wiki/Varnish_cache). This is easier to configure and change than coding a custom solution to extract static pages. IF this is for a performance issue. Or do you just want to distribute the pages like in a zip or something? Or is it for friendly URL? Commented Jun 12, 2014 at 23:12
  • If the promotion company truly believes that actually having HTML pre written for each product. Fire them and get a new company that has any clue what they are doing. If not, you can create pages that are rewritten using a template on the fly from DB information using URL rewrites. (Depending on server, but for Apache, look in to .htaccess and mod_rewrite) so that each product can emulate a search engine friendly URL like http://yoursite.com/products/fly_fish_reel and the .htaccess will rewrite it to /products.php?p=fly_fish_real or something similar. Commented Jun 12, 2014 at 23:18

1 Answer 1

3

The best way to do this, I think, would be to use use url rewrite to make it look like you have static pages when you don't. But if you were having database performance trouble and therefore wanted to cache a static copy of the file, you can also use the ob_ functions to cache a PHP file. Say for instance you only want to read the database once a day and cache the file, and the rest of the time just return the static cached file.

Pseudo-code for that:

$cached_file_path = 'some path for cached file';
//would use filemtime($cached_file_path) and time() to determine if file was
//cached today. could also do it every 3 hours, or whatever
If file has not been cached today or cachefile does not exist, then
{
    ob_start(); // starts recording the output in a buffer
    //do all your database reads and echos
    .....
    $contents = ob_get_contents(); // gets all the output for you to write into a file  
    //save contents to file at $cached_file_path
   ....
   ob_end_flush(); // returns the content to client
   exit;
}
else
{
    //just serve the cached file
    include($cached_file_path);
    exit;
}

Obviously you have to take parameters into account since they'll affect the name of the cache-file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.