html
var page = 2; //从第二页开始获取数据
// 点击查看更多
$("#getmoreare").click(function(){
$.ajax({
url: '地址',
type: 'POST',
dataType: 'json',
data: {page: page},
complete: function(xhr, textStatus) {
//called when complete
},
success: function(res) {
var str = "";//定义变量保存内容
$.each(res,function(index,array){
id = array['id'];
title = array['title'];
img = array['timage'];
alicon = array['content'];
time = array['createtime'];
if (array['pacbull']) {
$("#getmoreare").html("没有更多内容了..");
}else{
str += '<li>'
+'<a href="/Article/artdetail?id='+id+'">'
+'<div class="zizou_consulting_content_timg">'
+'<img src="'+img+'" alt="">'
+'</div>'
+'<div class="zizou_consulting_content_cen">'
+'<div>'
+'<h4>'+title+'</h4>'
+'</div>'
+'<div class="zizou_consulting_content_cen_removeHtmltag">'+alicon+'</div>'
+'<div class="zizou_consulting_content_cen_time">'+time+'</div>'
+'</div>'
+'</a>'
+'</li>';
}
})
$('.zizou_consulting_content ul:last').append(str);
var pageval = page++;//页数+1
$("#page").val(pageval);
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
});
})
后台
$where = [];
//获取请求页面数
$page = $this->request->post('page') ? $this->request->post('page') : 1;
$where = [
'switch'=>'1',
];
$num = 6;//请求条数
$more_data = amodel::where($where)->order('weigh','desc')->page($page,$num)->select()->toArray();
$count = count($more_data);
foreach ($more_data as $k=>$v){
$more_data[$k]['createtime'] = date('Y-m-d', $v['createtime']);
// 去除html标签,并截取40个字符,前台就不加载全部文章,以免增加耗时
$tagecontent = strip_tags($v['content']);
$pattern = '/\s/';//去除空白
$content = preg_replace($pattern, '', $tagecontent);
$more_data[$k]['content'] = mb_substr($content,0,40, "UTF-8");
}
if ($this->request->isPost()) {
if ($count < $num) {
$more_data[]['pacbull'] = '0';//到尾页返回0
}
return json($more_data);
exit;
}
$this->assign('more_data',$more_data);
$this->assign(['num'=>$num,'count'=>$count]);```