当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP+Mysql+jQuery实现动态展示信息

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

2019年04月19日  | 移动技术网IT编程  | 我要评论
在本站前面有文章介绍了如何实现发表微博说说:php+mysql+jquery实现发布微博程序--jquery篇,本例将基于其数据库结构,用动态的方式展示发表的说说信息。

查看示例:

xhtml
复制代码 代码如下:

<div id="demo">
<div class="saylist">
<a href="#"><img src="images/0.jpg" width="50" height="50" alt="demo" /></a>
<div class="saytxt">
<p><strong><a href="//www.jb51.net">shuro</a></strong><span>
8分钟前</span> 说:</p>
<div class="say">评论内容。。。</div>
</div>
</div>
...
</div>

上述html结构由n个.saylist构成,用于展示用户的评论信息,当然在本例中,将由php负责生成这段xhtml代码。
css
复制代码 代码如下:

#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}

使用上述css渲染html外观,当然你也可以自己定制你喜欢的外观样式。
php
在function.php中有两个函数,formatsay()用来输出用户评论列表,即输出上文中的html。
复制代码 代码如下:

function formatsay($say,$dt,$uid){
$say=htmlspecialchars(stripslashes($say));
return'
<div class="saylist"><a href="#"><img src="images/'.$uid.'.jpg" width="50"
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>';
}

时间轴函数trantime()将时间转换成如“1小时前”的格式,详情可阅读本站文章:php实现时间轴函数
复制代码 代码如下:

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 = '刚刚';
}
elseif ($time < 60 * 60) {
$min = floor($time/60);
$str = $min.'分钟前';
}
elseif ($time < 60 * 60 * 24) {
$h = floor($time/(60*60));
$str = $h.'小时前 '.$htime;
if($ds==1)
$str = '昨天 '.$rtime;
}
elseif ($time < 60 * 60 * 24 * 2) {
$str = '昨天 '.$rtime;
if($ds==2)
$str = '前天 '.$rtime;
}elseif($time < 60 * 60 * 24 * 3){
$str = '前天 '.$rtime;
if($ds>2)
$str = $rtime;
}
else {
$str = $rtime;
}
return $str;
}

然后在index.php中调用funciton.php,并连接mysql数据库输出评论列表。
复制代码 代码如下:

require_once('connect.php'); //连接数据库文件
require_once('function.php'); //函数文件
$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]);
}

在div#demo中输出评论列表。
复制代码 代码如下:

<div id="demo">
<?php echo $saylist;?>
</div>

这样一来,运行index.php会出现一个列表,我们只需要一条一条展示,下面就需要jquery来办了。
jquery
复制代码 代码如下:

$(function(){
//除了显示第一个saylist,其他的都隐藏
$(".saylist").hide().eq(0).show();
//自循环函数,循环展示信息
(function shownextsay(){
//每条信息展示7.5秒
$(".saylist:visible").delay(7500).fadeout("slow",function(){
$(this).appendto("#demo");
//展示下一条
$(".saylist:first").fadein("slow",function(){
//再次调用函数
shownextsay();
});
});
})();
});

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网