留言板
-------------------head.html---------------------------------------------------------------
<center>
<div>
<h1>留言板</h1>
<hr width="10%">
<a href="./form.php">添加留言</a> | <a href="">查看留言</a>
<hr/>
</div>
</center>
---------------------------form.php--------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#con{width:550px;height:300px;margin:0px auto; text-align:center;}
#con>form>input[type="submit"]{
width:100px;
height:30px;
position:relative;
left:205px;
}
</style>
</head>
<body>
<?php include ‘./public/head.html‘;?>
<div id="con">
<form action="write.php" method="post">
<span>用戶名:</span>
<input type="text" name="username" value="" placeholder="請輸入你的用戶名"/><br/>
<span>留言內容:</span>
<textarea name="content" id="" cols="50" rows="5"></textarea><br/>
<input type="submit" value="留言">
</form>
</div>
</body>
</html>
----------------------------------write.php------------------------------------------------
<?php
var_dump($_POST);
//1接收用戶名和留言信息
$user = empty($_POST[‘username‘])?‘遊客‘:$_POST[‘username‘];
$content = empty($_POST[‘content‘])?date(‘Y-m-d H:i:s‘):$_POST[‘content‘];
//echo $user;
//echo $content;
//2.寫入到文件中保存
//2.1 拼接字符串
// 將用戶名和留言內容鏈接成一個字符串(需要將字符串拼接成知道是誰留言的而且留言內容是什麽)
$str = $user.‘@@‘.$content.‘&&‘;
// echo $str;
//2.2 寫入文件保存
//2.2.1 打開文件
$handle=fopen(‘./mysql.txt‘,‘a‘);
//2.2.2 寫入文件
$int = fwrite($handle,$str);
//2.2.3 關閉文件
fclose($handle);
//3.判斷是否寫入成功
if($int >0){
//當你寫入成功的時候我們需要給出一個提示信息 並且跳轉到查看留言頁面
//echo‘留言成功‘;
echo ‘留言成功3s後跳轉頁面<meta http-equiv="refresh" content="3;url=./read.php"/>‘;
}else{
//寫入失敗的時候 提示寫入失敗 跳轉到留言頁面
//echo ‘留言失敗‘;
header(‘location:./form.php‘);
}
--------------------------------read.php---------------------------------------------------
<?php
//1.讀取mysql.txt文件裏面的內容
$content=file_get_contents(‘./mysql.txt‘);
//echo $content;
//2.遍歷到表格中
//2.1 將字符串分割為數組
$userinfo = explode(‘&&‘,$content);
//var_dump($userinfo);
//2.2 刪除數組中最後一個元素
array_pop($userinfo);
//var_dump($userinfo);
/***************************分頁樣式**********************************/
//1.定義每頁顯示條數
$page = 10;
//2.求出總頁數
//2.1 求出總條數
$total = count($userinfo);
//echo $total;
//2.2 計算總頁數
//總頁數
$amount = ceil($total/$page);
//echo $amount;
//3.獲取當前頁
$dpage = isset($_GET[‘page‘])?$_GET[‘page‘]:1;
//判斷當前範圍 當前頁碼不能小於1 也不能大於最大頁數
//處理上一頁
$prepage = $dpage-1<=1?1:($dpage-1);
//處理下一頁
$nextpage = $dpage+1>$amount?$amount:($dpage+1);
if($dpage<1){
$dpage=1;
}
if($dpage > $amount){
$dpage=$amount;
}
//4.獲取對應的頁數的數據
//求出每頁需要從第幾個下標開始取值
//0 10 20 30
// 1 2 3 4 (4-1)*10;
$num = ($dpage-1)*$page;
//開始獲取數據
$newinfo=array_slice($userinfo,$num,$page,true);
// var_dump($newinfo);
include ‘./public/head.html‘;
//2.3寫表格
echo ‘<table border="1" align="center" width="800">‘;
echo ‘<tr>‘;
echo ‘<th>編號</th>‘;
echo ‘<th>用戶名</th>‘;
echo ‘<th>留言內容</th>‘;
echo ‘<th>操作</th>‘;
echo ‘</tr>‘;
foreach($newinfo as $key=>$val){
//將我們得到的值進行分割因為值裏面包含兩個內容一個是用戶名一個是用戶留言信息 將其分割成為數組我們可以通過鍵值對的方式拿出裏面的值
$user = explode(‘@@‘,$val);
//var_dump($user);
//var_dump($val);
echo ‘<tr align="center">‘;
echo ‘<td>‘.$key.‘</td>‘;
echo ‘<td>‘.$user[0].‘</td>‘;
echo ‘<td>‘.$user[1].‘</td>‘;
echo ‘<td><a href="./del.php?id=‘.$key.‘">刪除</a>|<a href="./edit.php?id=‘.$key.‘">修改</a></td>‘;
echo ‘</tr>‘;
}
/*****************下面是分頁的樣式*************************/
echo ‘<tr>‘;
echo ‘<td colspan="4"align="right"><a href="read.php?page=1">首頁</a>|<a href="read.php?page=‘.$prepage.‘">上一頁</a>|<a href="read.php?page=‘.$nextpage.‘">下一頁</a>|<a href="read.php?page=‘.$amount.‘">尾頁</a></td>‘;
echo ‘</tr>‘;
echo ‘</table>‘;
-----------------------mysql.txt-----------------------------------------------------------
皮皮@@球球,我是皮皮呀 咱倆是一個組合呀&&球球@@皮皮,我不想要你了 我不想在成為一個球!&&哈哈哈哈@@呵呵呵呵&&薛琳@@薛琳還在裏面沒有出來呢 不是和你們說了嗎,酒駕十五天 我也很無奈!你們想就好了 我不想他 可算走了 沒有人打我 舒服&&@所有人@@各位 有沒有想要替代薛琳的 馬上報名 我來通過 你就是我邊上的這個人了!!!!&&xxx@@待遇豐厚 可以沒事欺負我!!&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&皮皮@@球球,我是皮皮呀 咱倆是一個組合呀&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&皮皮@@球球,我是皮皮呀 咱倆是一個組合呀&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&皮皮@@球球,我是皮皮呀 咱倆是一個組合呀&&12312312@@123123123&&12312312@@123123123&&12312312@@123123123&&111@@2222&&
----------------------------------del.php--------------------------------------------------
<?php
//將要刪除的數據下標傳遞過來,然後在本頁面中讀取文件獲取跟read.php頁面一樣的數組
//然後刪除這個數組中的下標,刪除之後在將數組拼接成為字符串之後寫入文件保存則是永久刪除
//刪除操作
//1.需要告訴我們要刪除誰
//var_dump($_GET);
if(isset($_GET[‘id‘])){
//說明你要刪除
//echo ‘刪除‘;
//讀取文件內容
$str = file_get_contents(‘./mysql.txt‘);
//echo $str;
$arr = explode(‘&&‘,$str);
//var_dump($arr);
//刪除傳入指定下標的數據
unset($arr[ $_GET[‘id‘] ]);
// var_dump($arr);
//將刪除後的數組 再次拼接成為字符串
$str = implode(‘&&‘,$arr);
//echo $str;
//將字符串以替換的方式寫入文件
//打開文件
$handle = fopen(‘./mysql.txt‘,‘w‘);
//寫
$int = fwrite($handle,$str);
if($int>0){
//echo ‘刪除成功‘;
header(‘location:./read.php‘);
}else{
//echo ‘刪除失敗‘;
header(‘location:./read.php‘);
}
}else{
//沒有給下標
echo ‘沒有刪除條件不能刪除‘;
}
------------------------------edit.php-----------------------------------------------------
<?php
var_dump($_GET);
//獲取數據下標
$id = $_GET[‘id‘];
//將文件中的內容拿出來
$content = file_get_contents(‘./mysql.txt‘);
//echo $content;
//將字符串變成數組
$arr = explode(‘&&‘,$content);
//獲取我們想要的字符串
$newstr = $arr[$id];
// echo $newstr;
//再次分隔得到用戶名和留言內容數組
$newarr = explode(‘@@‘,$newstr);
//將所有數據分隔出來 讓用戶名和留言內容分隔
var_dump($newarr);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#con{width:550px;height:300px;margin:0px auto; text-align:center;}
#con>form>input[type="submit"]{
width:100px;
height:30px;
position:relative;
left:205px;
}
</style>
</head>
<body>
<?php include ‘./public/head.html‘;?>
<div id="con">
<form action="doedit.php" method="post">
<input type="hidden" name="id" value="<?php echo $id?>">
<span>用戶名:</span>
<input type="text" name="username" value="<?php echo $newarr[0]?>" placeholder="請輸入你的用戶名"/><br/>
<span>留言內容:</span>
<textarea name="content" id="" cols="50" rows="5" ><?php echo $newarr[1]?></textarea><br/>
<input type="submit" value="修改">
</form>
</div>
</body>
</html>
--------------------------doedit.php-------------------------------------------------------
<?php
var_dump($_POST);
//接受修改頁面得到的數據
//下標
$id = $_POST[‘id‘];
//用戶名
$username = $_POST[‘username‘];
//留言內容
$contents = $_POST[‘content‘];
//讀取mysql.txt裏面的內容
$con = file_get_contents(‘./mysql.txt‘);
//echo $con;
//分隔成為數組
$arr = explode(‘&&‘,$con);
//將修改的字符串拼接成為一個字符串
var_dump($arr);
$str = $username.‘@@‘.$contents;
//echo $str;
//進行替換操作
$arr[ $id ] = $str;
//$arr[3] = ‘小王子@@我喜歡你好久了‘;
//var_dump($arr);
//得到新字符串
$newstr = implode(‘&&‘,$arr);
//echo $newstr;
//將新的字符串以替換的形式寫入到mysql.txt文件中
//1.打開文件
$file = fopen(‘./mysql.txt‘,‘w‘);
//2.寫入字符串
$int = fwrite($file,$newstr);
//判斷是否寫入
if($int>0){
echo ‘修改成功<a href="./read.php">返回</a>‘;
}else{
echo ‘修改失敗‘;
}
留言板