即时通讯使用的活动记录,它的所有工作确定,但我想将 $数据[totalres] 以总成绩,我的意思是,相同的查询,但没有在 LIMIT
问题是previous报表变得没有设置,当你做一个查询,修改,所以我甚至不能添加$这个 - > DB->限制()后,我得到的结果。
什么想法?我认为它是一种不好的做法,以复制的查询只是为了做到这一点
函数get_search($开始,$ numRows行,$过滤器=阵列())
{
...
$这个 - > DB
- >选择(EMP)
- 肽从('EMP')
- >加入('EMPR','empr.b = empr.id','左')
- >像('code',$code)
- >极限($ numRows行,$开始);
...
$ Q = $这个 - > DB->获得();
//行没有LIMIT X个,Y型过滤器
$数据[totalres] = ???????;
如果($ Q-> NUM_ROWS()大于0)
{
$数据[成果] = $ Q->的结果();
} 其他 {
$数据[成果] =阵列();
}
返回$的数据;
}
解决方案
您可以使用SQL_CALC_FOUND_ROWS得到的行数,将已经返回SANS - 限制
。注意,在
。这告诉codeIgniter不要试图逃脱反引号中的选择
行假 SELECT
子句(因为 SQL_CALC_FOUND_ROWS
不是一个领域,和codeIgniter没有意识到这一点)。
$这个 - > DB
- >选择(SQL_CALC_FOUND_ROWS EMP,FALSE)
- 肽从('EMP')
- >加入('EMPR','empr.b = empr.id','左')
- >像('code',$code)
- >极限($ numRows行,$开始);
$ Q = $这个 - > DB->获得();
然后在那之后查询被运行,我们需要运行另一个查询来获取的行的总数。
$查询= $这个 - > DB->查询(SELECT FOUND_ROWS()AS`Count`');
$数据[totalres] = $查询 - >排() - >计数;
im using active record, its all working ok but i want to set the $data["totalres"] to the total results, i mean, the same query but without the LIMIT
the problem is the previous statements gets unset when you do a query modifier, so i cant even add the $this->db->limit() after i get the results.
any ideas? i think its a bad practice to 'duplicate' the query just to do this
function get_search($start, $numrows, $filter = array())
{
...
$this->db
->select("emp")
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start);
...
$q = $this->db->get();
// number of rows WITHOUT the LIMIT X,Y filter
$data["totalres"] = ???????;
if ($q->num_rows() > 0)
{
$data["results"] = $q->result();
} else {
$data["results"] = array();
}
return $data;
}
解决方案
You can use SQL_CALC_FOUND_ROWS
to get the number of rows that would have been returned sans-LIMIT
. Note the ,FALSE
in the select
line. This tells CodeIgniter not to try to escape the SELECT
clause with backticks (because SQL_CALC_FOUND_ROWS
is not a field, and CodeIgniter doesn't realize that).
$this->db
->select("SQL_CALC_FOUND_ROWS emp", FALSE)
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start);
$q = $this->db->get();
Then after that query is ran, we need run another query to get the total number of rows.
$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
$data["totalres"] = $query->row()->Count;
相关推荐
最新文章