標籤:blog 分享 限制 -- 設定 class head 效果 登入
這裡只實現簡單的評論回複功能,並沒有設定使用者權限,關於許可權問題前邊的註冊登入已經詳細的介紹過,所以這裡不做贅述:
首先我們要在資料庫先建兩張表,分別是pinglun表與huifu表:
建立完兩張表後,開始寫評論模組,首先是建立表單,用文本域顯示評論內容,然後用按鈕提交到資料庫:
<form action="pingchuli.php" method="post"><input type="text" hidden="hidden" value="menghaoran" name="menghaoran" /><!--因為沒有許可權,這裡給了一個預設值--><div><textarea name="content"></textarea></div><br /><!--文本域評論顯示的地方--><input type="submit" value="評論" /></form>
然後寫評論的處理頁面pingchuli.php:
<?php$menghaoran = $_POST["menghaoran"];//擷取使用者$content = $_POST["content"];//擷取評論內容$time = date("Y-m-d H-i-s");//擷取目前時間echo "$time";require "DBDA.class.php";$db = new DBDA();$sql = "insert into pinglun values(‘‘,‘{$menghaoran}‘,‘{$content}‘,‘{$time}‘)";$arr=$db->query($sql,0);if($arr){ header("location:pinglun.php"); //返回首頁面}
看一下效果:
然後引入封裝類,寫sql語句查詢pinglun表,並遍曆出各項資料,然後重點是要在評論後進行回複,所以也要查詢huifu表遍曆出回複的資料,這裡雖然沒有使用者權限限制,但一定要擷取接收人的id:
<?phprequire "DBDA.class.php";$db = new DBDA();$sql= "select * from pinglun";$arr = $db->query($sql);foreach($arr as $v)//遍曆出評論表中的資料{echo"<tr> <td>{$v[1]}</td><br> <td>{$v[2]}</td><br> <td>{$v[3]}</td><br> <form action=‘huichuli.php?id={$v[0]}‘ method=‘post‘><textarea name=‘coment‘ ></textarea> <input type=‘submit‘ name=‘hui‘ value=‘回複‘/> </form> <tr>";//回複也要遍曆出來,而且要用表單包裹起來,因為要用到id$dc = new DBDA();$sql = "select * from huifu where jieshouid={$v[0]}";//找到接收者的id$arr = $dc->query($sql);foreach($arr as $v){ echo" <div style=‘color:red‘>{$v[2]}</div> <div style=‘color:red‘>{$v[3]}</div> <div style=‘color:red‘>{$v[4]}</div>";}}?>
然後是回複的處理頁面huichuli.php:
<?php$id = $_GET["id"];//將點擊回複的評論id傳過來$times = date("Y-m-d H-i-s");//擷取目前時間echo "$times";$coment = $_POST["coment"];$libai = "libai";//發送人預設值require "DBDA.class.php";$db = new DBDA();$sql = "insert into huifu values(‘‘,‘{$id}‘,‘{$libai}‘,‘{$times}‘,‘{$coment}‘)";//將資料添加到資料庫中$arr=$db->query($sql,0);if($arr){ header("location:pinglun.php"); //回到首頁面}
最終結果圖:
php評論回複功能