ds0409 2014-06-12 23:10
浏览 21
已采纳

从数据库内容创建静态HTML页面?

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?

  • 写回答

1条回答 默认 最新

  • doujiang2812 2014-06-12 23:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题