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


Create static HTML pages from database content?

我们最近为一个销售儿童玩具的客户建立了一个PHP电子商务网站,自从推出以来,他引入了一个推广机构,该机构建议为他的每个产品都建立静态HTML页面。他有超过2500个产品,所以手工构建每个产品是不现实的选择。

是否有可能用PHP从数据库中读取每个产品,并根据产品名称创建一个新文件,并用描述、图像和链接填充模板区域?如果是这样,有人建议我从哪里开始吗?

我认为,做到这一点的最好方法是使用url重写,使它看起来像你有静态页面,当你没有。但是,如果遇到数据库性能问题,因此希望缓存该文件的静态副本,那么也可以使用ob_函数来缓存PHP文件。例如,您只想每天读取数据库一次并缓存文件,其余时间只返回静态缓存文件。

伪代码

$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;
}

显然你必须考虑参数,因为它们会影响缓存文件的名称