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

管理你的留言在文本文件中实现不如数据库中那么方便,下面我们想办法来实现这个效果!
在输出的时候我们是用数组来保存数据,那么我们会很方便地为每组数据按顺序分配一个序号,通过对这个序号来操作,我们就可以方便地修改数据!
$date[0]为文本文件中第一行,我们输出时分配序号为0.
输出时有这样的效果:

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

我们再定制modify.php

1   
2if($id!=””){   
3$date=file(“txt/mytxt.txt”);   
4$str=explode(“&”,$date[0])   
1<form action="modify.php" method="post" onsubmit="return check()">
2<input $str[0];="" ```="" echo="" name="tech1" type="text" value="```"/>
3<input $str[1];="" ```="" echo="" name="tech2" type="text" value="```"/>
4<input $str[2];="" ```="" echo="" name="tech3" type="text" value="```"/>
5<input name="Submit" type="submit" value="Submit"/>
6</form>
1   
2}else{   
3$str=$tech1.”&”. $tech2. ”&”.$tech3….   
4If($str!=””){   
5//将修改后的数据写回文本文件!   
6}   
7}

在这个过程中要注意保持$id的值不丢失,在写回数据时先将文本中的值读入$date,再$date[$id]=$str;
$fp=fopen("txt/mytxt.txt","w");
fwrite($fp,$date);
fclose($fp);

Published At
Categories with Web编程
Tagged with
comments powered by Disqus