Php simple message board and reply function

Source: Internet
Author: User
Tags foreach comments php foreach

I found this tutorial on the Internet, but basically no one said anything. Then one day I saw a PHP program designing a database design similar to this, and I had a lot of insights! The following figure shows the structure of the database.

 

Next, let's continue with the introduction.

Id: this is the parent Id. You can use this id to query whether there is a child id under this id. You can also record the id of the message.

Son_id: this is a child id, and the corresponding parent id can be found through this id.
News_id record the document id
Sender_author: the person who receives the message
Receiver_author: the person who sent the message
Content
Status when someone replies to your post, the value is changed from 0 to 1, and the message is not displayed. If you do not click in, the message still exists.
Time records the message time

 Message BoardReply to ideas

 When the sender (the person who sends the message) wants to leave a message or reply to the post, the receiver (the author of the article or the person to be replied) is required to receive the message. If the single database statement finds the keys sender_author and receiver_author, it will know who the receiver is and who the sender is. Then the response content is displayed based on this judgment.

 

  Prompt you to have a new message idea

When the sender sends a messageThe default value of status is 0, indicating that you receive a new message. When you click in, call the database modification statement to change the specified value to 1.                  

 

Database operation statement for viewing comments

 

The code is as follows: Copy code

 

Function message ($ id ){
$ Query = $ this-> db-> query ("SELECT * FROM message WHERE news_id = '$ ID'"); // query all the comments in the article id
Return $ query-> result ();
 }

 

This is the code for getting the parent message content

The code is as follows: Copy code


<? Php foreach ($ query as $ sel) {?>
<P> message User: <? Php echo $ sel-> receiver_author;?> Message content: <? Php echo $ sel-> content?> </P>
<? Php }?>

 

Verify whether the user logs in and gives the message permission

 

The code is as follows: Copy code

 

<Form id = "form1" name = "form1" method = "post" action = "<? Php echo site_url (). "/publish/user_message"?> ">
<Textarea rows = "5" cols = "50" name = "huifu" <? Php if ($ uere_name = "0") {echo "disabled" ;}?> >
<? Php
If ($ uere_name = "0 ")
{Echo "Sorry, you have not logged on yet and cannot leave a message ";}
?> Www.111cN.net
</Textarea>
<Input class = "wole" name = "author" value = "<? Php echo $ author;?> "/> <! -- Recipient's Post author -->
<Input class = "wole" name = "news_id" value = "<? Php echo $ news_idx;?> "/> <! -- Article id -->
<Input type = "submit" name = "Submit"/>
</Form>
<Script language = "javascript">
Function updateinfo (){
If (<? Php echo $ uere_name;?> = 1 ){
Document. form1.Submit. value = "Message ";
Document. form1.Submit. disabled = false;
  }
Else {
Document. form1.Submit. value = "not logged on ";
Document. form1.Submit. disabled = "disabled ";
  }
}
Updateinfo ();
</Script>

 

This shows all the child IDs and message content in the specified parent id.

 

The code is as follows: Copy code

 

<P> Here is <? Php echo $ is;?> Floor User: <? Php echo $ sel-> receiver_author;?> <Br/> message content: <? Php echo $ sel-> content?> & Nbsp;

<A onClick = "showdiv ('contentid <? Php echo $ is;?> ', 'Showtext <? Php echo $ is;?> ') "Href =" javascript: void (0) "> Reply </a>
<Div id = "contentid <? Php echo $ is;?> "Class =" none ">
<? Php
$ Query = $ this-> db-> query ("select * from message where son_id = '$ sel-> ID' order by id "); // Obtain the subreply of the specified parent id
$ Revis = $ query-> result ();
Foreach ($ revis as $ row) {?>
<P> <? Php if ($ row-> sender_author ==$ row-> receiver_author) {echo $ row-> sender_author ;}
Else {echo $ row-> sender_author. "replied:". $ row-> receiver_author ;}?>
Content: <? Php echo $ row-> content?> </P>
<? Php }?>
<Form action = "<? Php echo site_url (). "/publish/son_message"?> "Method =" post ">
<Input name = "son_idx" class = "wole" value = "<? Php echo $ sel-> id?> "/>
<Input name = "receiver_author" class = "wole" value = "<? Php echo $ sel-> receiver_author;?> "/>
<Input class = "wole" name = "news_id" value = "<? Php echo $ news_idx;?> "/> <! -- Article id -->
<Textarea rows = "5" cols = "50" name = "huifux"> </textarea>
<Br> <input type = "submit" name = "sub" value = "reply"> </form> </div> </p>
<Script language = "JavaScript" type = "text/JavaScript">
<! --Www.111cn.net
Function showdiv (targetid, objN ){
  
Var target = document. getElementById (targetid );
Var clicktext = document. getElementById (objN)
     
If (target. style. display = "block "){
Target. style. display = "none ";
Clicktext. innerText = "reply ";
 

} Else {
Target. style. display = "block ";
Clicktext. innerText = 'collapse ';
            }
  
}
-->
</Script>

 

This is the effect after implementation. The image Message Board has implemented message content reply.

Then, when you reply to you, a new message is displayed.

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.