db类怎样先获得总行数,再输出一个分页而不重新构造查询
本帖最后由 uicestone 于 2012-10-27 09:39 编辑上代码
$this->db->select('express.id,express.destination,express.content,express.comment,express.time_send,express.num,staff.name AS sender_name')
->from('express')
->join('staff','staff.id=express.sender','left')
->where('express.display',1);
$rows=$this->db->count_all();//得到上述sql语句对象查询的总行数
print_r($this->db);//此时发现对象中德sql已经置空
$data=$this->db->limit(30,0)->get()->result();//试图加上分页并输出一页数据,但是失败了,因为在此之前sql对象已经为空
我尝试复制db对象
$db=$this->db->select('express.id,express.destination,express.content,express.comment,express.time_send,express.num,staff.name AS sender_name')
->from('express')
->join('staff','staff.id=express.sender','left')
->where('express.display',1);
$rows=$db->count_all();//得到上述sql语句对象查询的总行数
print_r($this->db);//此时对象中的sql还是已经置空
$data=$this->db->limit(30,0)->get()->result();//仍然是空的查询
count_all_results
手册是用来看的啦 jeongee 发表于 2012-10-27 10:10 static/image/common/back.gif
count_all_results
手册是用来看的啦
不好意思问题打错了,我程序里用的是count_all_results,手册当然看过了,没有说count_all_results()以后sql对象被清空了,怎么在加分页条件输出啊。。
请求大大仔细看一下我的问题 本帖最后由 曜日晨阳 于 2012-10-27 15:48 编辑
$db = $this->db->select('express.id,express.destination,express.content,express.comment,express.time_send,express.num,staff.name AS sender_name')->join('staff', 'staff.id=express.sender', 'left')->where('express.display', 1);
$return = function($value = '', $offset = '') {
if($value == '' && $offset == ''){
return $this->db->get('express')->num_rows();
}
return $this->db->get('express', $offset, $value)->result();
};
$row = $return();
$obj = $return(30,0);
uicestone 发表于 2012-10-27 10:16 static/image/common/back.gif
不好意思问题打错了,我程序里用的是count_all_results,手册当然看过了,没有说count_all_results()以后 ...
我现在的解决方法是用对象复制'clone',复制一个sql对象用来计算总行数。 uicestone 发表于 2012-10-27 10:16 static/image/common/back.gif
不好意思问题打错了,我程序里用的是count_all_results,手册当然看过了,没有说count_all_results()以后 ...
我没说错啊
还是仔细看手册
把条件存储到数组里
$conditions = array();
$total = $this->db->where($conditions)->count_all_results('test');
$data['companies'] = $this->db->from('test')->where($conditions)->limit($limit)->offset($offset)->order_by('id', 'DESC')->result();
//而且,你还可以利用这个数组进行附加分页参数
你说是不是呢?
页:
[1]