PHP + Mysql + jQuery implement dynamic display of information _ PHP Tutorial

Source: Internet
Author: User
PHP + Mysql + jQuery enables dynamic display of information. Previous articles on this site introduced how to implement the microblog: PHP + Mysql + jQuery to implement the microblog program-jQuery. This example is based on its database structure, this article describes how to implement the microblog: PHP + Mysql + jQuery to implement the microblog program-jQuery. This example is based on its database structure, displays the Presentation Information dynamically.

View example: DEMO

XHTML

The code is as follows:






Shuro
8 minutes ago:


Comment...




...



The above HTML structure consists of N. saylist, which is used to display the user's comment. In this example, PHP is responsible for generating the XHTML code.
CSS

The code is as follows:


# Demo {width: 400px; height: 80px; margin: 80px auto; border-bottom: 1px dotted # d3d3d3}
. Saylist {margin: 8px auto; height: 80px; padding: 4px 0 ;}
. Saylist img {float: left; width: 50px; margin: 4px}
. Saytxt {float: right; width: 320px; overflow: hidden}
. Saytxt p {line-height: 18px}
. Saytxt p strong {margin-right: 6px}
. Saytxt p span {color: #999}
. Say {margin-top: 3px; font-size: 14px; font-weight: bold}


If you use the preceding CSS to render the HTML appearance, you can also customize the style you like.
PHP
There are two functions in function. php. formatSay () is used to output the user comment list, that is, output the HTML in the above section.

The code is as follows:


Function formatSay ($ say, $ dt, $ uid ){
$ Say = htmlspecialchars (stripslashes ($ say ));
Return'

Height = "50" alt = "demo"/>


Demo _ '. $ uid .''. TranTime ($ dt).' says:

'. $ Say .'




';
}


The time axis function tranTime () converts the time to a format such as "1 hour ago". for details, see this article: PHP implementation of the time axis function.

The code is as follows:


Function tranTime ($ stime ){
$ Rtime = date ("m-d H: I", $ stime );
$ Htime = date ("H: I", $ stime );
$ Day_time = date ("j", $ stime );
$ Today = date ("j", time ());
$ Ds = $ today-$ day_time;
$ Time = time ()-$ stime;
If ($ time <60 ){
$ Str = 'gang ';
}
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 before '. $ htime;
If ($ ds = 1)
$ Str = 'Yesterday '. $ rtime;
}
Elseif ($ time <60*60*24*2 ){
$ Str = 'Yesterday '. $ rtime;
If ($ ds = 2)
$ Str = 'day before Day'. $ rtime;
} Elseif ($ time <60*60*24*3 ){
$ Str = 'day before Day'. $ rtime;
If ($ ds> 2)
$ Str = $ rtime;
}
Else {
$ Str = $ rtime;
}
Return $ str;
}


Call funciton. php in index. php and connect to the MySQL database to output the comment list.

The code is as follows:


Require_once ('connect. php'); // connect to the database file
Require_once ('function. php'); // function file
$ Query = mysql_query ("select * from say order by id desc limit 0, 15 ");
While ($ row = mysql_fetch_array ($ query )){
$ SayList. = formatSay ($ row [content], $ row [addtime], $ row [userid]);
}


Output the comment list in p # demo.

The code is as follows:






In this way, a list will appear when you run index. php. we only need to display the list one by one, and jQuery is required below.
JQuery

The code is as follows:


$ (Function (){
// Except for displaying the first saylist, all other items are hidden.
$ (". Saylist"). hide (). eq (0). show ();
// The self-cyclic function displays information cyclically.
(Function showNextSay (){
// Each message is displayed for 7.5 seconds.
$ (". Saylist: visible"). delay (7500). fadeOut ("slow", function (){
$ (This). appendTo ("# demo ");
// Display the next
$ (". Saylist: first"). fadeIn ("slow", function (){
// Call the function again
ShowNextSay ();
});
});
})();
});

...

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.