PHP + Mysql + jQuery enables dynamic display of information

Source: Internet
Author: User

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, displays the presentation information dynamically.

View Example: DEMO

XHTML
Copy codeThe Code is as follows:
<Div id = "demo">
<Div class = "saylist">
<A href = "#"> </a>
<Div class = "saytxt">
<P> <strong> <a href = "http://www.jb51.net"> Shuro </a> </strong> <span>
8 minutes ago </span> said: </p>
<Div class = "say"> comment... </Div>
</Div>
</Div>
...
</Div>

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
Copy codeThe 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.
Copy codeThe Code is as follows:
Function formatSay ($ say, $ dt, $ uid ){
$ Say = htmlspecialchars (stripslashes ($ say ));
Return'
<Div class = "saylist"> <a href = "#"> Height = "50" alt = "demo"/> </a>
<Div class = "saytxt">
<P> <strong> <a href = "#"> demo _'. $ uid. '</a> </strong> <span> '. tranTime ($ dt ). '</span>:
</P> <div class = "say"> '. $ say.' </div>
</Div>
<Div class = "clear"> </div>
</Div> ';
}

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.
Copy codeThe 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.
Copy codeThe 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 div # demo.
Copy codeThe Code is as follows:
<Div id = "demo">
<? Php echo $ sayList;?>
</Div>

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
Copy codeThe 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.