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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等