我与最新的工作codeIgniter
发布,我也有工作jquery
datatables
从datatables.net
我已经写了这个功能: https://gist.github.com/4478424其中,按原样工作正常。 除了当我用文本框中键入的东西过滤器,过滤器本身发生了,但我的计算是完全关闭。
我尝试添加在$res = $this->db->count_all_results()
我之前get
,并从所有工作停止GET。 我需要完成,什么if ($data['sSearch'] != '')
然后利用整个查询无limit
,看看有多少总行与搜索过滤器存在。
如果你需要看的比什么其他的任何其他代码在我的主旨,就问我,我会继续和张贴。
Answer 1:
$this->db->count_all_results()
代替$this->db->get()
在数据库中调用。
IE,你可以调用count_all_results()
或get()
,但不能同时使用。
你需要做两个不同的活动记录的调用。 一个分配结果#,以及一个获得实际效果。
事情是这样的计数:
$this->db->select('id');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();
而对于实际的查询(你应该已经有了):
$this->db->select($your_columns);
$this->db->from('table');
$this->db->where($your_conditions);
$this->db->limit($limit);
$query = $this->db->get();
Answer 2:
你有没有在读了https://www.codeigniter.com/userguide2/database/active_record.html#caching ?
我看你正在尝试做一些分页,你需要“真正的”总结果,并在同一时间限制。
这是我实践我的大部分代码我做CI。
$this->db->start_cache();
// All your conditions without limit
$this->db->from();
$this->db->where(); // and etc...
$this->db->stop_cache();
$total_rows = $this->db->count_all_results(); // This will get the real total rows
// Limit the rows now so to return per page result
$this->db->limit($per_page, $offset);
$result = $this->db->get();
return array(
'total_rows' => $total_rows,
'result' => $result,
); // Return this back to the controller.
我打了上面的代码未经测试,但它应该工作是这样的。 我这样做,我的所有项目。
Answer 3:
该
$this->db->count_all_results();
实际上取代了:
$this->db->get();
所以,你不能真正兼得。
如果你想确实有get和你可以很容易地做到这一点相同的查询来计算NUM行:
$this->db->from(....);
$this->db->where(....);
$db_results = $this->get();
$results = $db_results->result();
$num_rows = $db_results->num_rows();
Answer 4:
实际上,你不必须有从任一,您可以在像这样的count_all_results表名。
$this->db->count_all_results('table_name');
Answer 5:
试试这个
/**
* @param $column_name : Use In Choosing Column name
* @param $where : Use In Condition Statement
* @param $table_name : Name of Database Table
* Description : Count all results
*/
function count_all_results($column_name = array(),$where=array(), $table_name = array())
{
$this->db->select($column_name);
// If Where is not NULL
if(!empty($where) && count($where) > 0 )
{
$this->db->where($where);
}
// Return Count Column
return $this->db->count_all_results($table_name[0]);//table_name array sub 0
}
然后简单的调用方法
像这样
$this->my_model->count_all_results(['column_name'],['where'],['table name']);
Answer 6:
与no_reset_flag第一计数。
... $this->db->count_all_results('', FALSE); $rows = $this->db->get()->result_array();
Answer 7:
如果查询包含GROUP BY,使用count_all_results失败。 我写了一个简单的方法来解决这个问题。 防止写你的查询两次关键是把它们都可以被调用两次的私有方法中。 下面是一些示例代码:
class Report extends CI_Model { ... public function get($page=0){ $this->_complex_query(); $this->db->limit($this->results_per_page, $page*$this->results_per_page); $sales = $this->db->get()->result(); //no table needed in get() $this->_complex_query(); $num_results = $this->_count_results(); $num_pages = ceil($num_results/$this->results_per_page); //return data to your controller } private function _complex_query(){ $this->db->where('a', $value); $this->db->join('(subquery) as s', 's.id = table.s_id'); $this->db->group_by('table.column_a'); $this->db->from('table'); //crucial - we specify all tables here } private function _count_results(){ $query = $this->db->get_compiled_select(); $count_query = "SELECT count(*) as num_rows FROM (".$query.") count_wrap"; $r = $this->db->query($count_query)->row(); return $r->num_rows; } }
文章来源: codeigniter count_all_results