Php+mysql+jquery implementation of the release of the microblog program PHP chapter, mysqljquery_php Tutorial

Source: Internet
Author: User
Tags php define

Php+mysql+jquery implementation of the release of the microblog program PHP, Mysqljquery


Let's start by explaining the business process for this example:
1, the front-end user input content, and the input of the content of the words in real-time statistics.
2, the user submits the data, jquery realizes through the Ajax sends the data in the background.
3, the background PHP receives the data submitted form, and the data for the necessary security filtering.
4. Background PHP connects to MySQL database and writes the submitted form data to the corresponding data table.
5, backstage to return the successful result data content, and through Ajax to insert the returned data content into the front page.
The above 1, 2 steps in the previous article: The jquery chapter has been explained, this article will complete the rest of the walk.

Data Sheet
First we want to prepare a data table, the table structure is as follows:

CREATE TABLE ' say ' (  ' id ' int (one) not null auto_increment,  ' userid ' int (one) ' NOT null default ' 0 ',  ' content ' VA Rchar (+) is not null,  ' addtime ' int (ten) is not NULL,  

Note that in this example, the Time field: Addtime type is set to int, it is convenient for subsequent time processing, and in many applications (such as the Discuz Forum), the time type is converted to a digital type.
Time-axis processing functions and formatted output list functions:
The timeline handler function is to convert the time to what we see in the form of "5 minutes Ago", "Yesterday 10:21" and the code as follows:

/* Time conversion function */function Trantime ($time) {  $rtime = date ("m-d h:i", $time);  $htime = Date ("H:i", $time);  $time = Time ()-$time;   if ($time <) {   $str = ' just ';  }  ElseIf ($time <) {   $min = floor ($time/60);   $str = $min. ' Minutes ago ';  }  ElseIf ($time < *) {   $h = floor ($time/(60*60));   $str = $h. ' Hour ago '. $htime;  }  ElseIf ($time < * 3) {   $d = floor ($time/(60*60*24));   if ($d ==1)    $str = ' Yesterday '. $rtime;   else    $str = ' The day before yesterday '. $rtime;  }  else {   $str = $rtime;  }  

The formatted output function is the function to output the user information and the published content and time in a certain format to the front page, the code is as follows:

function Formatsay ($say, $dt, $uid) {  $say =htmlspecialchars (stripslashes ($say));   Return      

demo_ '. $uid. ' '. Preg_replace ('/(?: http|https|ftp): \/\/(?: [a-z0-9][a-z0-9_-]* (?: \. [A-z0-9] [a-z0-9_-]*) +):? (\d+)? \/? [^\s\ "\ ']+)/I ', ' $ ', $say). '

'. Trantime ($DT). '

Put all two of these functions into function.php, ready to be called at any time.
submit.php processing form data
In the previous article, we know that jquery submits the data from the front end to the submit.php in the background via Ajax. Then submit is to complete all the following tasks. Please look at the code:

Require_once (' connect.php '); Database connection file require_once (' function.php '); Function call file  $txt =stripslashes ($_post[' saytxt ');//Get Submitted Data $txt =mysql_real_escape_string (Strip_tags ($txt), $link ); Filter the HTML tags and escape the special characters if (Mb_strlen ($txt) <1 | | Mb_strlen ($txt) >140) die  ("0");//Determine whether the number of input characters meets the requirements $time =time (); Gets the current time $userid =rand (0,4); Insert data into the data table $query =mysql_query ("INSERT into say (userid,content,addtime) VALUES (' $userid ', ' $txt ', ' $time ')"); if (Mysql_affected_rows ($link)!=1)  

Note that in this case, the user ID (userid) is randomly processed for demonstration purposes, and the actual application is to get the ID of the current user. In addition to the database connection files, you can write their own, in the download I provided the demo also has this file.
Finally, go back to the front page index.php. Index.php mainly in addition to provide input entry, but also to take the background processing the returned results, and to the data in the database to display. The code is as follows:

<?php define (' Include_check ', 1); Require_once (' connect.php '); Require_once (' function.php ');  $query =mysql_query ("SELECT * from say ORDER BY id DESC limit 0,10"); while ($row =mysql_fetch_array ($query)) {  $sayList. =formatsay ($row [content], $row [Addtime], $row [UserID]);}? >    <?php echo $sayList;? >  

The above is the whole content of this article, I hope that everyone's study has helped.

http://www.bkjia.com/PHPjc/1061524.html www.bkjia.com true http://www.bkjia.com/PHPjc/1061524.html techarticle Php+mysql+jquery Implementation of the release of the micro-blogging program PHP, Mysqljquery first or to illustrate the business process of this example: 1, the front-end user input content, and the input of the content of the words in real-time statistics. ...

  • 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.