PHP + Mysql + jQuery implement microblog program php

Source: Internet
Author: User
Tags php define
This article mainly introduces PHP + Mysql + jQuery to publish Weibo programs. It mainly introduces how the backend processes the data submitted at the front end and returns the results. For more information, see

This article mainly introduces PHP + Mysql + jQuery to publish Weibo programs. It mainly introduces how the backend processes the data submitted at the front end and returns the results. For more information, see

First, describe the business process of this example:
1. The frontend user inputs the content and counts the number of words in the input content in real time.
2. When users submit data, jQuery sends data to the background through Ajax.
3. the backend PHP receives the data in the submitted Form and performs necessary security filtering on the data.
4. the backend PHP connects to the Mysql database and writes the submitted form data to the corresponding data table.
5. The background will return the successful result data content, and insert the returned data content to the front-end page through Ajax.
Steps 1 and 2 are explained in the previous article: jQuery, And the rest of this article will be completed.

:

Data Table
First, we need to prepare a data table. The table structure is as follows:

Create table 'say' ('id' int (11) not null auto_increment, 'userid' int (11) not null default '0', 'content' varchar (200) not null, 'addtime' int (10) not null, primary key ('id') ENGINE = MyISAM default charset = utf8;

Note: In this example, the time field 'addtime' type is set to int' to facilitate subsequent time processing. In many applications (such as the Discuz Forum), the time field is converted to the numeric type.
Timeline processing function and formatting output list function:
The timeline processing function converts the time into the formats we see, such as "5 minutes ago" and "yesterday". The Code is 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 <60) {$ str = 'hangzhou ';} elseif ($ time <60*60) {$ min = floor ($ time/60); $ str = $ min. 'minute ago ';} elseif ($ time <60*60*24) {$ h = floor ($ time/(60*60); $ str = $ h. 'hour ago '. $ htime;} elseif ($ time <60*60*24*3) {$ d = floor ($ time/(60*60*24 )); if ($ d = 1) $ str = 'Yesterday '. $ rtime; else $ str = 'day before yesterday '. $ rtime;} else {$ str = $ rtime;} return $ str ;}

The formatting output function is a function that outputs user information, published content, and time to the front-end page in a certain format. 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', '$ 1', $ say ).'

'. TranTime ($ dt ).'

';}

Put the above two functions into function. php and prepare to be called at any time.
Submit. php processes form data
In the previous article, we learned that jQuery submitted the data obtained by the front-end to submit. php In the background using the POST method. Then, submit is to complete all subsequent tasks. See the Code:

Require_once ('connect. php '); // database connection file require_once ('function. php '); // function call File $ txt = stripslashes ($ _ POST ['saytxt']); // obtain submitted data $ txt = mysql_real_escape_string (strip_tags ($ txt), $ link); // filter HTML tags and escape special characters if (mb_strlen ($ txt) <1 | mb_strlen ($ txt)> 140) die ("0"); // determines whether the number of input characters meets the requirements $ time = time (); // obtain the current time $ userid = rand (); // 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) die ("0"); echo formatSay ($ txt, $ time, $ userid); // call the function to output the result

Note: In this example, the user ID (userid) is randomly processed for demonstration. The actual application is to obtain the ID of the current user. In addition, you can write a database connection file by yourself. This file is also available in the downloaded DEMO provided by me.
Finally, return to index. php on the front-end page. Index. php not only provides the input entry, but also processes the returned results in the background and displays the existing data in the database. 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 all the content of this article, hoping to help you learn.

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.