用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2467|回复: 5
收起左侧

[HELP] db类怎样先获得总行数,再输出一个分页而不重新构造查询

[复制链接]
发表于 2012-10-27 09:36:00 | 显示全部楼层 |阅读模式
本帖最后由 uicestone 于 2012-10-27 09:39 编辑

上代码
PHP复制代码
 
$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对象
PHP复制代码
 
$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();//仍然是空的查询
 
复制代码




发表于 2012-10-27 10:10:46 | 显示全部楼层
count_all_results
手册是用来看的啦
 楼主| 发表于 2012-10-27 10:16:09 | 显示全部楼层
jeongee 发表于 2012-10-27 10:10
count_all_results
手册是用来看的啦

不好意思问题打错了,我程序里用的是count_all_results,手册当然看过了,没有说count_all_results()以后sql对象被清空了,怎么在加分页条件输出啊。。
请求大大仔细看一下我的问题
发表于 2012-10-27 10:23:53 | 显示全部楼层
本帖最后由 曜日晨阳 于 2012-10-27 15:48 编辑
PHP复制代码
$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);
复制代码


 楼主| 发表于 2012-10-27 10:27:51 | 显示全部楼层
uicestone 发表于 2012-10-27 10:16
不好意思问题打错了,我程序里用的是count_all_results,手册当然看过了,没有说count_all_results()以后 ...

我现在的解决方法是用对象复制'clone',复制一个sql对象用来计算总行数。
发表于 2012-10-27 10:32:16 | 显示全部楼层
uicestone 发表于 2012-10-27 10:16
不好意思问题打错了,我程序里用的是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();
//而且,你还可以利用这个数组进行附加分页参数
你说是不是呢?

本版积分规则