生成数据 -> 读取模板 -> 替换标签 ->写入html文件

解决方案 »

  1.   

    那個分頁的我還不會呀。
    http://www.ybfq.net/show.asp?id=1176
    http://www.ybfq.net/show.asp?id=1162
      

  2.   

    生成分页的
    <?  $connect=mysql_connect('localhost','root','root') or die('无法连接服务器!');  $fn="temp.html";
        $fp=fopen($fn,"r");
        $tent=fread($fp,filesize($fn));$select=mysql_select_db('nihao',$connect);//选择数据库
        /* if(empty($start)){
     $start=0;//分页开始的记录数
      $page=1;}//当前页数 */
           $sql="select * from ly order by id desc";//sql查询语句
          $query0=mysql_query($sql,$connect);
      $num=mysql_num_rows($query0);
      $psize=2;
      $pcunt=ceil($num/$psize);//总页数
    //  echo $pcunt;
     /* if($num%$psize!=0) 
      $lastpagerecord=$num%$psize;//求得最后一页的记录数
      else $lastpagerecord=$psize;
      $sqlstr="select * from ly order by id desc limit $start,$psize";
      $query=mysql_query($sqlstr,$connect);  */
        for($i=0;$i<$pcunt;$i++)
      {if($i==0){$indexpath="index.html"; }  else {
      $indexpath="index_".$i.".html"; }
      echo  $indexpath;
      $start=$i*$psize;
      $file="";
       $sqlstr="select * from ly order by id desc limit $start,$psize";
      $query=mysql_query($sqlstr,$connect);
     while($array=mysql_fetch_array($query)){
        $file=$file."<tr><td>".$array["fbr"]."</td><td>".$array["email"]."</td></tr>";
     }
         // echo $file; echo "<br>";
        
    $nume="";
    for($j=1;$j<=$pcunt;$j++)
      {
                if($j==$i+1) $nume.=$j."\n";
              elseif($j==1)    {$nume.="<a href=index.html>".$j."</a>\n";}
    else {$n=$j-1;
          $nume.="<a href=index_".$n.".html>".$j."</a>\n";   }
      }
      $content=str_replace("{name}",$file,$tent);
     // $content=str_replace("{title}",$title,$content);
        $content=str_replace("{nume}",$nume,$content);//echo $content;
     
      $filename="123/".$indexpath;
    if (is_file ($filename)){ 
            @unlink ($filename);//若文件已存在,则删除
         }
          
      $handle=fopen($filename,"w"); //打开文件指针,创建文件
      /*
     检查文件是否被创建且可写
      */
      if (!is_writable($filename)){ 
         echo "文件:".$filename."不可写,请检查其属性后重试!";
      }
      if (!fwrite ($handle,$content)){  //将信息写入文件
         echo "生成文件".$filename."失败<br>";
      } 
      else  echo "生成文件".$filename."成功<br>";
      fclose ($handle); //关闭指针
       }
           fclose ($fp);
    ?>   
      

  3.   

    麻烦 ashchen(陈辉)再指点一下:我从数据库读取数据怎样生成?
    我用fwrite:
    $filename="temp.html";
    $handle="<html><body>hello</body></html>";
     if (!$fp= fopen($filename, 'a+')) {
             print "不能打开文件 $filename";
             exit;
        }
        if (!fwrite($fp, $handle)) {
            print "不能写入到文件 $filename";
            exit;
        }因为我对这方面一点经验都没有,所以毫无头绪。
      

  4.   

    如果要生成多个htm文件,那生成一个html文件后怎么清空模扳列?
      

  5.   

    我不明白你说的是什么。我具体的作法是这样的。
    在文件头加上
    ob_start();
    文件尾加上
    $str=ob_get_clean();
    然后把$str的内容存成一个html文件就可以了。没有什么模板。就是把一个php文件当前的内容写成静态。
      

  6.   

    你难道没有用模扳替换生成html文件吗?
      

  7.   

    那我这样生成多个html文件应该可以把$nid=$_REQUEST["download"];
    db_connect();
    $sql="select * from article where net_id='".$nid."'";
    $result=db_query($sql);
    $houzui=".html";
    $file_dir="/CMS/netpage/'".$uid."'/'".$nid."'";
    while($row=mysql_fetch_assoc($sql))
    {
    $num=$row["art_id"];
    $content=$row["content"]; 
    $titile=$row["title"];
    $artkey=$row["art_key"];
    $path=$file_dir."/".$num.$houzui;
    $fp=fopen(“model.htm”,”r”); 
    ob_start();
    $str=fread($fp,filesize(“mode.htm”));
    $str=str_replace(“{title}”,$title,$str); 
    $str=str_replace(“{content}”,$content,$str); 
    $str=str_replace(“{artkey}”,$artkey,$str);
    fclose($fp); 
    $handle=fopen($path,”w”);
    fwrite($handle,$str);
    $str=ob_get_clean();
    fclose($handle); 
    }
      

  8.   

    ob_start();
    ...
    $str=ob_get_clean();的作用是将(...)里echo的内容全部存进$str里去。
    这样你只要正常的写php动态页面就可以生成静态的页了。没有模板。用模板是另一种方法。不是我这种。
      

  9.   

    那用模板生成html后是怎么清空模板列?
      

  10.   

    不知道你想干什么?也不知道你遇到了什么问题?生成静态页面的过程就是把原来动态页面的结果保存到指定的文件中去
    $s = file_get_contents('http://localhost/page.php?id=1234');
    file_put_contents('1234.htm', $s); //file_put_contents只在php5中有,php4中可以自己写一个这与是否使用模板无关
    当然你需要修改文件中的链接,以免造成空链或仍指向php页面。这才是最重要的!