<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php生成静态html分页实现方法</title>
</head><body><?php
$conn=mysql_connect('localhost','root','123456')
  or  die('连接失败:'.mysql_error());
//选择数据表
if (mysql_select_db('bbs',$conn))
  {
  echo'选择数据库成功!'.'<p>';
  
  }
  else
  {
  echo'数据库选择失败!'.mysql_error().'<p>';
  }
@header("Content-Type: text/html; charset=utf-8");
mysql_query("SET NAMES 'utf8'");
 
$fp = fopen ("moban/temp.html","r");
$content = fread ($fp,filesize ("moban/temp.html"));
$onepage =2;
$sql = "select id from message";
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);
$filedir="news_1";
if (!is_dir($filedir)) {
//如果不存在就建立
mkdir($filedir,0777);
}
for ($i = 0;$i<$allpages; $i++){
if ($i == 0){
$indexpath = "$filedir/index.html";
} else {
$indexpath = "$filedir/index_".$i.".html";
}
$start = $i * $onepage;
$list = '';
$sql_for_page = "select * from message limit $start,$onepage";
$result=mysql_query($sql_for_page);
 while($row=mysql_fetch_array($result))
 {
 $list .=$row['id']."&nbsp;&nbsp;&nbsp;<a href=../".$row['filepath'].">".$row['title'].'</a><br>'.$row['content'].'<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 ("生成分页文件完成,如生成不完全,请检查文件权限系统后重新生成!");
?>
</body>
</html>

解决方案 »

  1.   

    <?php
    /*
    $fp=fopen("tmp.htm","r");//只读打开模板
    $str=fread($fp,filesize("tmp.htm"));//读取模板中内容
    //下一步将要替换的内容替换成要生成的内容
    $str=str_replace("{title}",'新标题',$str);//将title替换成新标题
    $str=str_replace("{content}",'新内容',$str);//将content替换成新内容
    fclose($fp);//关闭文件$handle=fopen('news.htm',"w");//写入方式打开新闻路径
    fwrite($handle,$str);//把刚才替换的内容写进生成HTM文件
    fclose($handle);
    echo "生成成功";    
    */
    //批量生成  
    $row=array (array ("新闻标题","新闻内容"),array("新闻标题2","新闻内容2"));//二维数组
    foreach($row as $id => $val){
     $title=$val[0];
     $content=$val[1];
     $path=$id.'.htm';
     $fp=fopen("tmp.htm","r");//只读打开模板
     $str=fread($fp,filesize("tmp.htm"));
     //$str=file_get_contents("tmp.htm","r");
     $str=str_replace("{title}",$title,$str);
     $str=str_replace("{content}",$content,$str);//替换内容
     fclose($fp);
     
     $handle=fopen($path,"w");//写入方式打开新闻路径
    fwrite($handle,$str);//把刚才替换的内容写进生成HTM文件
    fclose($handle);
    echo "生成成功";
     }
    // unlink("0.htm"); //括号只要输入要删除的文件名即可。
    ?>
      

  2.   

    我要的不是这样,,我只要生成静态分页,因为上面的分页代码是一个for循环出来的,,本来可以判断5页就跳出循环,,,可以跳出循环后面的分页就没有了...
      

  3.   

    wp.qq.com进这个把那段js添到你要显示的位子,也不是你首页的模板文件中
      

  4.   

    我用的服务器是windows 7系统
      

  5.   

    在数据表更新,删除,新增时,把涉及到的HTML文件删除,
    当请求到该HTML页面时,如果页面不存在,再新建