源码下载地址
思路
表单有标题和内容,文件为index.html.表单数据提交到liuyan.php文件,liuyan.php文件把数据写入到msg.txt文件中。msg.php文件为首页,展示标题列表。当点击标题列表进入readmsg.php文件中,找到tid=$_GET[‘tid’]的那行内容并展示出来。
留言表单
一窥其貌:
// index.html
<form action="liuyan.php" method="post">
<p>留言标题:<input type="text" name="title" /></p>
<p>留言内容:
<textarea name="content" cols="30" rows="10"></textarea>
</p>
<input type="submit" value="提交" />
<a href="msg.php">返回</a>
</form>
接收表单数据的PHP文件
涉及的知识点:
PHP Filesystem 函数
php打开文件fileopen简写为fopen
$fh = fopen('./msg.txt','a');
// f h 是 一 个 资 源 类 型 的 变 量 ‘ f w i r t e ( fh是一个资源类型的变量 `fwirte( fh是一个资源类型的变量‘fwirte(fh, ‘from php into txt’);// 沿着$fh资源管道写内容
fclose($fh);` // 关闭资源
// liuyan.php
<?php
header("Content-type: text/html; charset=utf-8");
//开始留言
$str = $_POST['title'].",".$_POST['content']."\n";
$fh = fopen('./msg.txt', 'a');
fwrite($fh, $str);
fclose($fh);
echo "ok",'<br />';
echo '<a href="index.html">','留言','</a>';
echo '  <a href="msg.php">','首页','</a>';
?>
首页msg.php
一窥其貌:标题列表
涉及到的知识点:
fgetcsv基础
csv是什么文件格式
csv文件
header("Content-type: text/html; charset=utf-8");
$fh = fopen('./msg.txt', 'r');
$i = 1;
while (($row= fgetcsv($fh))!= false) {
echo '<li><a href="readmsg.php?tid=',$i,'">',$row[0],'</a></li>';
$i = $i + 1;
}
详情页readmsg.php
header("Content-type: text/html; charset=utf-8");
$tid = $_GET['tid'];
//echo '你想看第',$tid,'行留言';
//打开得到文件
$fh = fopen('./msg.txt', 'r');
$i = 1;
while (($row= fgetcsv($fh))!= false) {
if ($i == $tid) {
// print_r($row);
echo '<h1>',$row[0],'</h1>';
echo "<p>".$row[1]."</p>";
echo '<a href="index.html">','留言','</a>';
echo '  <a href="msg.php">','首页','</a>';
}
$i = $i + 1;
}