PHP + Mysql + jQuery: php for microblogs, mysqljquery_PHP tutorial

Source: Internet
Author: User
Tags php define
PHP + Mysql + jQuery: publish the microblog program php, mysqljquery. PHP + Mysql + jQuery implements the release of the microblog program php. mysqljquery should first describe the business process in this example: 1. the front-end user inputs the content and counts the number of words entered in real time. PHP + Mysql + jQuery: php for microblogs, mysqljquery

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.

Mysqljquery describes the business process in this example: 1. the frontend user inputs and counts the number of words entered in real time ....

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.