用文本文件制作留言板提示(下)

  管理你的留言在文本文件中实现不如数据库中那么方便,下面我们想办法来实现这个效果!

  在输出的时候我们是用数组来保存数据,那么我们会很方便地为每组数据按顺序分配一个序号,通过对这个序号来操作,我们就可以方便地修改数据!

  $date[0]为文本文件中第一行,我们输出时分配序号为0.

  输出时有这样的效果:

  <a href=”modify.php?Id=0'>修改留言</a>

  我们再定制modify.php

  <?php

  if($id!=””){

  $date=file(“txt/mytxt.txt”);

  $str=explode(“&”,$date[0])

  ?>

  <form method="post" action="modify.php" onsubmit="return check()">

  <input type="text" name="tech1" value=<?php echo $str[0];?>>

  <input type="text" name="tech2" value=<?php echo $str[1];?>>

  <input type="text" name="tech3" value=<?php echo $str[2];?>>

  <input type="submit" name="Submit" value="Submit">

  </form>

  <?php

  }else{

  $str=$tech1.”&”. $tech2. ”&”.$tech3….

  If($str!=””){

  //将修改后的数据写回文本文件!

  }

  }?>

  在这个过程中要注意保持$id的值不丢失,在写回数据时先将文本中的值读入$date,再$date[$id]=$str;

  $fp=fopen("txt/mytxt.txt","w");

  fwrite($fp,$date);

  fclose($fp);