PHP Comment reply function

Source: Internet
Author: User

Here only to implement a simple comment reply function, and do not set the user rights, about the permissions issue in front of the registration login has been detailed introduction, so here do not repeat:

First we need to build two tables in the database, the Pinglun table and the Huifu table, respectively:

After setting up two tables, we begin to write a comment module, first to create a form, to display a comment in a text field, and then to submit it to a database using a button:

<formAction= "pingchuli.php"Method= "POST"><inputtype= "text"Hidden= "hidden"value= "Menghaoran"name= "Menghaoran" /><!--because there is no permission, a default value is given here. -<Div><textareaname= "Content"></textarea></Div><BR/><!--where text field comments are displayed -<inputtype= "Submit"value= "comment" /></form>

Then write a comment on the processing page pingchuli.php:

<?PHP$menghaoran=$_post["Menghaoran"];//Get user$content=$_post["Content"];//Get Comment content$time=Date("y-m-d h-i-s");//Get current TimeEcho"$time";require"DBDA.class.php";$db=NewDbda ();$sql= "INSERT into Pinglun values ('", ' {$menghaoran}‘,‘{$content}‘,‘{$time}‘)";$arr=$db->query ($sql, 0);if($arr){    Header("location:pinglun.php");//back to Main page}

Look at the effect:

Then introduce the encapsulation class, write the SQL statement query Pinglun table, and traverse out the various data, and then focus on the comment after the reply, so also to query the Huifu table to traverse out the reply data, although there is no user permission limit, but must get the recipient's ID:

<?PHPrequire"DBDA.class.php";$db=NewDbda ();$sql= "SELECT * FROM Pinglun";$arr=$db->query ($sql);foreach($arr  as $v)//iterate through the data in a comment table{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= ' reply '/> </form> <tr>";//replies are also traversed and wrapped with a form, because the ID$DC=NewDbda ();$sql= "SELECT * from Huifu where jieshouid={$v[0]} ";//find the recipient's 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> ";}}?>

Then the reply processing page huichuli.php:

<?PHP$id=$_get["id"];//Pass the comment ID of the click Reply .$times=Date("y-m-d h-i-s");//Get current TimeEcho"$times";$coment=$_post["Coment"];$libai= "Libai";//Sender Default Valuerequire"DBDA.class.php";$db=NewDbda ();$sql= "INSERT into Huifu values ('", ' {$id}‘,‘{$libai}‘,‘{$times}‘,‘{$coment}‘)";//to add data to the database$arr=$db->query ($sql, 0);if($arr){    Header("location:pinglun.php");//back to Main page}

Final result diagram:

PHP Comment reply function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.