注:参考兄弟连教学视频写的一个简单的留言板模块,获得更多内容请参考我的博文。
示例:文本式留言板
需要的知识点:
1.文件操作:
file_put_contents();文件的写入函数
file_get_contents();文件的读取函数
2.字符串的处理函数
explode();将字串拆分成数组的函数
implode();将数组以指定分割符合并成字串的函数
3.数组
foreach() 遍历数组
unset() 销毁变量
全局数组:
$_POST[]
$_SERVER["REMOTE_ADDR"];//获取客户端的IP地址
time();//获取当前系统的时间戳
date();日期转换函数;
示例的目录结构:
====================================
|--index.php 添加留言信息界面
|
|--doAdd.php获取留言信息,并执行添加操作的php文件
|
|--show.php显示留言信息的界面
|
|--del.php 执行删除留言信息的界面
|
|--liuyan.txt 用于储存留言信息的文件
代码实现:
<html>
<head>
<title>我的留言板</title>
</head>
<body>
<center>
<h2>我的留言板</h2>
<a href = "index.php">添加留言</a>
<a href = "show.php" >查看留言</a>
<hr width = "90%">
<h3>添加留言</h3>
<form action = "doAdd.php" method = "post">
<table width = "380" border = "0" cellpadding = "4">
<tr>
<td align = "right">标题:</td>
<td><input type = "text" name = "title"></td>
</tr>
<tr>
<td align = "right">留言者:</td>
<td><input type = "text" name = "author"></td>
</tr>
<tr>
<td align = "right" valign = "top">留言内容:</td>
<td><textarea name = "content" row = "5" cols = "30"></textarea></td>
</tr>
<tr>
<td colspan = "2" align = "center"><input type = "submit" value = "提交">
<input type = "reset" value = "重置"></td>
</tr></table>
</form>
</center>
</body>
</html>
doAdd.php 添加留言部分
<html>
<head>
<title>我的留言板</title>
</head>
<body>
<center>
<h2>我的留言板</h2>
<a href = "index.php">添加留言</a>
<a href = "show.php" >查看留言</a>
<hr width = "90%">
<h3>添加留言</h3>
<?php
//执行留言信息添加操作
//1.获取要添加的留言信息,并补上其他辅助信息(ip地址、添加时间)
$title = $_POST["title"];
$author = $_POST["author"];
$content = $_POST["content"];
$ip = $_SERVER["REMOTE_ADDR"];
$addtime = time();
//2.拼装留言信息
$ly = "{$title}##{$author}##{$content}##{$ip}##{$addtime}@@@";
//echo $ly;
//3. 将留言添加到liuyan.txt文件中
$info = file_get_contents("liuyan.txt");
file_put_contents("liuyan.txt",$info.$ly);
echo "</br>";
//file_put_contents("liuyan.txt",$ly); 直接输出会覆盖上一条留言!
//4.输出留言成功!
echo "留言成功!";
?>
</center>
</body>
</html>
show.php 留言显示部分:
<html>
<head>
<title>我的留言板</title>
<script>
function dodel(id){
if(confirm("确定要删除么?"))
{
window.location ='del.php?id='+id;
}
}
</script>
</head>
<body>
<center>
<h2>我的留言板</h2>
<a href = "index.php">添加留言</a>
<a href = "show.php" >查看留言</a>
<hr width = "90%">
<h3>查看留言</h3>
<table border = "1" width = "700" >
<tr>
<th>留言标题</th>
<th>留言人</th>
<th>留言内容</th>
<th>IP地址</th>
<th>留言时间</th>
<th>操作</th>
</tr>
<?php
// 获取留言信息,解析后输出到表格中
// 1.从留言liuyan.txt中获取留言信息
$info = file_get_contents("liuyan.txt");
// 2.去除留言内容最后的三个@@@符号
$info = rtrim($info,"@");
if(strlen($info)>=8){
// 3.以@@@符号拆分留言信息为一条一条的(将留言信息以@@@符号拆分成留言数组)
$lylist = explode("@@@",$info);
// 4.遍历留言信息数组,对每条留言做再次解析;
foreach($lylist as $k=>$v){
$ly = explode("##",$v);
echo "<tr>";
echo "<td>{$ly[0]}</td>";
echo "<td>{$ly[1]}</td>";
echo "<td>{$ly[2]}</td>";
echo "<td>{$ly[3]}</td>";
echo "<td>".date("Y-m-d H:i:s",$ly[4])."</td>";
echo "<td><a href = 'javascript:dodel({$k})'>删除</a></td>";
}
}
?>
</center>
</body>
</html>
del.php 留言删除部分:
<html>
<head>
<title>我的留言板</title>
</head>
<body>
<center>
<h2>我的留言板</h2>
<a href = "index.php">添加留言</a>
<a href = "show.php" >查看留言</a>
<hr width = "90%">
<h3>删除留言</h3>
<?php
//1. 获取要删除留言号
$id = $_GET["id"];
// 2.从留言liuyan.txt中获取留言信息
$info = file_get_contents("liuyan.txt");
//3.(将留言信息以@@@符号拆分成留言数组)
$lylist = explode("@@@",$info);
//4.使用unset删除指定的id留言
unset($lylist[$id]);
//还原留言信息为字串,并写回留言文件
$newinfo = implode("@@@",$lylist);
file_put_contents("liuyan.txt",$newinfo);
echo "删除成功!";
?>
</center>
</body>
</html>