使用帝国实现新闻的异步加载
1.将getMore.php放置e/action目录下,实现功能,从新闻表中不断的读取数据,提供给前台。
<?php
require('../class/connect.php');
require('../class/db_sql.php');
require('../data/dbcache/class.php');
if($_POST[action] == 'getmorenews'){
$table=htmlspecialchars($_POST[table]);
if(empty($_POST[orderby])){$orderby='newstime';}else{ $orderby=htmlspecialchars($_POST[orderby]);}
if(empty($_POST[myorder])){$myorder='desc';}else{ $myorder='asc';}
if(empty($_POST[limit])){$limit=15;}else{ $limit=(int)$_POST[limit];}
if(empty($_POST[classid])){$where=null;}else{ $where='where classid in('.$_POST[classid].')';}
if(empty($_POST[length])){$length=50;}else{ $length=(int)$_POST[length];}
if(empty($_POST[small_length])){$small_length=500;}else{ $small_length=(int)$_POST[small_length];}
$link=db_connect();
$empire=new mysqlquery();
$num =(int)$_POST['next'] *$limit;
if($table){
$sql=$empire->query("SELECT * FROM `".$dbtbpre."ecms_".$table."` $where order by $orderby $myorder limit $num,$limit");
while($r=$empire->fetch($sql)){
if($r[titlepic]==''){
$r[titlepic]="/images/news/2.jpg";
}
$oldtitle=stripSlashes($r[title]);
$title=sub($oldtitle,'',$length);
$smalltext=stripSlashes($r[smalltext]);
$smalltext=sub($smalltext,'',$small_length);
$classname=$class_r[$r[classid]][classname];
$newsurl=$public_r[newsurl];
$classurl=$newsurl.$class_r[$r[classid]][classpath];
$titleurl=$r[titleurl];
echo ' <a href="'.$titleurl.'" class="weui-media-box weui-media-box_appmsg">
<div class="weui-media-box__hd">
<img class="weui-media-box__thumb" src="'.$r[titlepic].'" alt="">
</div>
<div class="weui-media-box__bd">
<h4 class="weui-media-box__title">'.$title.'</h4>
<span>'.date('Y-m-d',$r[newstime]).'</span>
<p class="weui-media-box__desc">'.$smalltext.'</p>
</div>
</a>';
}
}
}
db_close();
$empire=null;
?>
2,前台不断的请求使用ajax实现具体代码如下
$(function(){
var i = 1; //设置当前页数
$('#loadmore').click(function(){
$.ajax({
url : '/e/action/getMore.php',
type:'POST',
data:{"next":i,'table':'news','action':'getmorenews','limit':15,'small_length':3000},
dataType : 'html',
beforeSend:function(){
$("#loadmore").show().html('Get More...');
$('#loadmore').attr('disabled','disabled');
},
success : function(data){
if(data){
$(".news-list").append(data);
$("#loadmore").removeAttr('disabled');
$("#loadmore").html('More');
i++;
}else{
$("#loadmore").show().html("No More News");
$('#loadmore').attr('disabled','disabled');
return false;
}
}
});
});
});
效果如图