我们最近为一位销售 child 玩具的客户构建了一个 PHP 电子商务网站,自推出以来,他聘请了一家促销机构,他们建议为他的每件产品提供静态 HTML 页面。他拥有超过 2,5000 种产品,因此手动构建每一种产品都是不现实的选择。
是否可以使用 PHP 从数据库中读取每个产品并根据产品名称创建一个新文件并使用描述、图像和链接填充模板区域?如果是这样,有人对我可以从哪里开始有任何建议吗?
最佳答案
我认为最好的方法是使用 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;
}
显然,您必须考虑参数,因为它们会影响缓存文件的名称。
关于php - 从数据库内容创建静态 HTML 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24195288/