繁体   English   中英

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

[英]Create static HTML pages from database content?

最近,我们为销售儿童玩具的客户建立了一个PHP电子商务网站,自从他成立以来,他已经引进了一家促销机构,该机构建议为每个产品都提供静态HTML页面。 他拥有超过25,000种产品,因此手动构建每个产品都不是现实的选择。

PHP是否可以从数据库中读取每个产品,并根据产品名称创建一个新文件,并在模板区域填充说明,图像和链接? 如果有的话,关于我从哪里开始有任何建议吗?

我认为,执行此操作的最佳方法是使用use url rewrite使它看起来像没有静态页面时一样。 但是,如果您在数据库性能方面遇到问题,因此想缓存文件的静态副本,则还可以使用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;
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM