php生成静态html分页实现方法
<?php
$conn=mysql_connect('localhost','root','wy')
or die('连接失败:'.mysql_error());
//选择数据表
if (mysql_select_db('mynews',$conn))
{
echo'选择数据库成功!'.'<p>';
}
else
{
echo'数据库选择失败!'.mysql_error().'<p>';
}
@header("Content-Type: text/html; charset=utf-8");
mysql_query("SET NAMES 'utf8'");
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$onepage =2;
$sql = "select id from news";
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);
for ($i = 0;$i<$allpages; $i++){
if ($i == 0){
$indexpath = "index.html";
} else {
$indexpath = "index_".$i.".html";
}
$start = $i * $onepage;
$list = '';
$sql_for_page = "select * from news limit $start,$onepage";
$result=mysql_query($sql_for_page);
while($row=mysql_fetch_array($result))
{
$list .= 'uid='.$row['id'].$row['title'].'<br>';
}
$content1 = str_replace ("{ articletable }",$list.$i,$content);
//分页
$list1 = '';
for ($j = 0;$j<$allpages; $j++){
if ($j == 0){
$list1 .= '<a href="index.html" >第'.$j.'页 </a>|';
} else {
$list1 .= "<a href='index_".$j.".html' >第".$j."页 </a>|";
}
}
$content2 = str_replace ("{ mune }",$list1,$content1);
if (is_file ($indexpath)){
@unlink ($indexpath); //若文件已存在,则删除
}
$handle = fopen ($indexpath,"w"); //打开文件指针,创建文件
/*
检查文件是否被创建且可写
*/
if (!is_writable ($indexpath)){
echo "文件:".$indexpath."不可写,请检查其属性后重试!"; //修改为echo
}
if (!fwrite ($handle,$content2)){ //将信息写入文件
echo "生成文件".$indexpath."失败!"; //修改为echo
}
fclose ($handle); //关闭指针
}
fclose ($fp);
die ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");
?>
temp.html
<HTML>
<TITLE>{ title }</TITLE>
<BODY>
this is a { file } file's templets
{ articletable }
分页 { mune }
</BODY>
</HTML>
相关文章
php 多维数组筛选判断2020-12-27
php查询某个表的数据总量代码2020-02-29
php+jquery+ajax+json的一个最简单实例2019-06-18