《Page.php》
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Page {
public $pageNow = 1; //当前页
public $pageCount = 100;//数据总条数
public $pageSize = 3;//每页显示条数
//获取分页
public function getPage(){
$total = ceil($this->pageCount/$this->pageSize);//总页数
//页码起始数
$start = $this->pageNow - 3;
$start = $start < 1 ? 1 : $start;
//页码结束数
$end = $start + 4;
$end = $end > $total ? $total : $end;
//生成页码数
$str = '';//页码
for($i = $start; $i <= $end;$i++){
if($this->pageNow == $i){
$str .= '<span>'.$i.'</span> ';
}else{
$str .= '<a href="javascript:page('.$i.')">'.$i.'</a> ';
}
}
// $str .= ' | '.' '.'总页数'.' '.$total;
return $str;
}
}
------------------------------------------------------------------------------------------ ↑ 封装分页类 ↑ ----------------------------------------------------------------------------------------------------------------------
《controller.php》
public function getAjax()
{
$param = $this->input->get();
//获取数据总条数
$this->page->pageCount = $this->zufang->getfang($param,'',1);
$p = 1;
if(!empty($param['p']))
{
$p = $param['p'];
}
//当前页
$this->page->pageNow = $p;
//当前偏移量
$limit[] = ($p-1)*$this->page->pageSize;
//每页显示条数
$limit[] = $this->page->pageSize;
//获取数据
$data['data'] = $this->zufang->getfang($param,$limit);
$data['page'] = $this->page->getPage();
exit(json_encode($data));
}
------------------------------------------------------------------------------------------ ↑ 控制器↑ ----------------------------------------------------------------------------------------------------------------------
《model.php》
/**
* 搜索 getfang($param)
*/
public function getfang($param = array(),$limit = array(0,4),$count = false)
{
//过滤where条件
if(!empty($param['title']))
{
$sql = ' (title like "%'. $param['title'].'%" )';
$this->db->where($sql);
}
if(!empty($param['address']))
{
$sql = ' (address like "%'. $param['address'].'%" )';
$this->db->where($sql);
}
if(!empty($param['house']))
{
$sql = ' (house like "%'. $param['house'].'%" )';
$this->db->where($sql);
}
if(!empty($param['room']))
{
$sql = ' (room like "%'. $param['room'].'%" )';
$this->db->where($sql);
}
if(!empty($param['price']))
{
$sql = ' (price like "%'. $param['price'].'%" )';
$this->db->where($sql);
}
if(!empty($param['facilities']))
{
$sql = ' (facilities like "%'. $param['facilities'].'%" )';
$this->db->where($sql);
}
if(!empty($param['contact']))
{
$sql = ' (contact like "%'. $param['contact'].'%" )';
$this->db->where($sql);
}
//区间搜索
if(!empty($param['start_price'])){
$this->db->where('price >=',$param['start_price']);
}
if(!empty($param['end_price'])){
$this->db->where('price <=', $param['end_price']);
}
//排除关键字
if(!empty($param['unkeyword'])){
$sql = ' (goods_name not like "%'. $param['unkeyword'].'%" AND goods_desc not like "%'.$param['unkeyword'].'%")';
$this->db->where($sql);
}
// //查询字段
$this->db->select();
//查询表
$this->db->from('zufang');
// //查询返回资源
// $query = $this->db->get();
//查询总条数
if($count)
{
return $this->db->count_all_results();
}
//limit查询 第二个参数返回的是 偏移量
$this->db->limit($limit[1],$limit[0]);
//查询返回资源
$query = $this->db->get();
//返回数组形式
$data = $query->result_array();
// var_dump($data);exit;
return $data;
}
------------------------------------------------------------------------------------------ ↑ 模型层↑ ----------------------------------------------------------------------------------------------------------------------