<?php
include("conn.php");
$db=db_connect();
$sql="select content from content";
$rs=$db->query($sql);
$row=mysqli_fetch_row($rs);
foreach($row as $id=>$val ){
  $content=$val; 
  $path=$id.'.htm';
  $fp=fopen("tmp.htm","r"); //只读打开模板
  $str=fread($fp,filesize("tmp.htm"));//读取模板中内容  $str=str_replace("{content}",$content,$str);//替换内容
  fclose($fp);  $handle=fopen($path,"w"); //写入方式打开新闻路径
  fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件
  fclose($handle);
  }?>这个代码在循环数据的那个地方有问题,生成的页面只有一个,高手给改下代码
谢谢啦

解决方案 »

  1.   

    <?php
    include("conn.php");
    $db=db_connect();
    $sql="select content from content";
    $rs=$db->query($sql);
    $row=mysqli_fetch_row($rs);
    $fp=fopen("tmp.htm","r"); //只读打开模板
    $str1=fread($fp,filesize("tmp.htm"));//读取模板中内容foreach($row as $id=>$val ){
      $content=$val; 
      $path=$id.'.htm';
      $str=$str1;  
      $str=str_replace("{content}",$content,$str);//替换内容
      
      $handle=fopen($path,"w"); //写入方式打开新闻路径
      fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件
      fclose($handle);
      }fclose($fp);
    ?>
      

  2.   

    <?php 
    include("conn.php"); 
    $db=db_connect(); 
    $sql="select content from content"; 
    $rs=$db->query($sql); 
    $fp=fopen("tmp.htm","r"); //只读打开模板 
    $str1=fread($fp,filesize("tmp.htm"));//读取模板中内容 while($row=mysqli_fetch_row($rs)){ 
      $content=$$row['id']; 
      $path=$row['id'].'.htm'; 
      $str=$str1;  
      $str=str_replace("{content}",$content,$str);//替换内容 
      
      $handle=fopen($path,"w"); //写入方式打开新闻路径 
      fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件 
      fclose($handle); 
      } fclose($fp); 
    ?> 
      

  3.   

    你只open了一个文件,怎么会有两个静态页?
      

  4.   

    <?php
    include("conn.php");
    $db=db_connect();
    $sql="select content from content";
    $rs=$db->query($sql);
    while ($row=mysqli_fetch_array($rs))
    {
      $content=$row[1].$row[2].$row[3]; 
      $path=$row[0].'.htm';
      $fp=fopen("tmp.htm","r"); //只读打开模板
      $str=fread($fp,filesize("tmp.htm"));//读取模板中内容  $str=str_replace("{content}",$content,$str);//替换内容
      fclose($fp);  $handle=fopen($path,"w"); //写入方式打开新闻路径
      fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件
      fclose($handle);

    }
    ?>
    你那一段代码,是按数据库的里的字段生成的,所以我看到生成了几个文件
    $row=mysqli_fetch_row($rs);你这个本来就是取一行当然只有一个文件了
      

  5.   

    $row=mysqli_fetch_row($rs);你这个本来就是取一行当然只有一个文件了 
    mysqli_fetch_array($rs)取的是你多行数据
    你查一下手册就知道了
    我是我的本地数据和你的数据是不一样的当然出错了
    $content=$row[1].$row[2].$row[3]; 
    $path=$row[0].'.htm';

    $row[0]也就是你的表中的第一个字段,$[1],$[2].....
    也可以用$row["字段名"]如$row["id"]
      

  6.   

    LZ去看看手册,mysqli_fetch_row是怎么用的,#2已经帮你写了。
    还有,你要生成多个不同的文件,就在while循环里fopen("不同的文件名","..").