PHP+Mysql+jQuery实现动态展示信息

jerry mysql 2015年08月20日 收藏

在本文中,我将介绍如何在页面上实现动态展示用户发表的信息,将用户发表的信息逐条播放展示。该效果可以在展示系统动态、商品评论等场景应用。

在本站前面有文章介绍了如何实现发表微博说说:PHP+Mysql+jQuery实现发布微博程序--jQuery篇,本例将基于其数据库结构,用动态的方式展示发表的说说信息。

HTML

  1. <div id="demo">
  2. <div class="saylist">
  3. <a href="#"><img src="images/0.jpg" width="50" height="50" alt="demo" /></a>
  4. <div class="saytxt">
  5. <p><strong><a href="com">月光光</a></strong><span>
  6. 8分钟前</span> 说:</p>
  7. <div class="say">评论内容。。。</div>
  8. </div>
  9. </div>
  10. ...
  11. </div>

上述HTML结构由N个.saylist构成,用于展示用户的评论信息,当然在本例中,将由PHP负责生成这段XHTML代码。

CSS

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

使用上述CSS渲染HTML外观,当然你也可以自己定制你喜欢的外观样式。

PHP

在function.php中有两个函数,formatSay()用来输出用户评论列表,即输出上文中的HTML。

  1. function formatSay($say,$dt,$uid){
  2. $say=htmlspecialchars(stripslashes($say));
  3. return'
  4. <div class="saylist"><a href="#"><img src="images/'.$uid.'.jpg" width="50"
  5. height="50" alt="demo" /></a>
  6. <div class="saytxt">
  7. <p><strong><a href="#">demo_'.$uid.'</a></strong> <span>'.tranTime($dt).'</span> 说:
  8. </p><div class="say">'.$say.'</div>
  9. </div>
  10. <div class="clear"></div>
  11. </div>';
  12. }

时间轴函数tranTime()将时间转换成如“1小时前”的格式,详情可阅读本站文章:PHP实现时间轴函数

  1. function tranTime($stime) {
  2. $rtime = date("m-d H:i",$stime);
  3. $htime = date("H:i",$stime);
  4. $day_time = date("j",$stime);
  5. $today=date("j",time());
  6. $ds = $today - $day_time;
  7. $time = time() - $stime;
  8. if ($time < 60) {
  9. $str = '刚刚';
  10. }
  11. elseif ($time < 60 * 60) {
  12. $min = floor($time/60);
  13. $str = $min.'分钟前';
  14. }
  15. elseif ($time < 60 * 60 * 24) {
  16. $h = floor($time/(60*60));
  17. $str = $h.'小时前 '.$htime;
  18. if($ds==1)
  19. $str = '昨天 '.$rtime;
  20. }
  21. elseif ($time < 60 * 60 * 24 * 2) {
  22. $str = '昨天 '.$rtime;
  23. if($ds==2)
  24. $str = '前天 '.$rtime;
  25. }elseif($time < 60 * 60 * 24 * 3){
  26. $str = '前天 '.$rtime;
  27. if($ds>2)
  28. $str = $rtime;
  29. }
  30. else {
  31. $str = $rtime;
  32. }
  33. return $str;
  34. }

然后在index.php中调用funciton.php,并连接MySQL数据库输出评论列表。

  1. require_once('connect.php'); //连接数据库文件
  2. require_once('function.php'); //函数文件
  3. $query=mysql_query("select * from say order by id desc limit 0,15");
  4. while ($row=mysql_fetch_array($query)) {
  5. $sayList.=formatSay($row[content],$row[addtime],$row[userid]);
  6. }

在div#demo中输出评论列表。

  1. <div id="demo">
  2. <?php echo $sayList;?>
  3. </div>

这样一来,运行index.php会出现一个列表,我们只需要一条一条展示,下面就需要jQuery来办了。

jQuery

  1. $(function(){
  2. //除了显示第一个saylist,其他的都隐藏
  3. $(".saylist").hide().eq(0).show();
  4. //自循环函数,循环展示信息
  5. (function showNextSay(){
  6. //每条信息展示7.5秒
  7. $(".saylist:visible").delay(7500).fadeOut("slow",function(){
  8. $(this).appendTo("#demo");
  9. //展示下一条
  10. $(".saylist:first").fadeIn("slow",function(){
  11. //再次调用函数
  12. showNextSay();
  13. });
  14. });
  15. })();
  16. });

下载地址