【问题标题】:Codeigniter dynamic pagination and where_not_inCodeigniter 动态分页和 where_not_in
【发布时间】:2014-06-23 11:45:24
【问题描述】:

所以我的模型中有一个函数,它可以为分页表提取作业,但前提是它们没有被预订。

我的第一个函数计算有多少页等的结果。

function record_count() {
  $this->db->select('id');
  $this->db->from('jobs');
  $this->db->where('`id` NOT IN (SELECT `jobs_id` FROM `jobs_user`)', NULL, FALSE);
  return $this->db->count_all("jobs");
}

我的第二个实际上带来了工作。

function getPagedJobs($limit, $start){
  $this->db->limit($limit, $start);
  $grabPagedJobs = $this->db->where('`id` NOT IN (SELECT `jobs_id` FROM `jobs_user`)', NULL, FALSE)->get("jobs");

  if ($grabPagedJobs->num_rows() > 0) {
    foreach ($grabPagedJobs->result() as $row) {
      $data[] = $row;
    }
    return $data;
  }
  return false;
}

问题是第二个函数可以很好地处理作业,并且不显示已预订的作业。但是计算页面的第一个函数似乎仍然计算所有结果,甚至是预订的结果。因此在我的分页结果上给了我空白页面和 PHP 无效参数。

我已经搞砸了很多方法,如果可能的话,我更喜欢主动记录,但我没有成功。

谢谢,

【问题讨论】:

    标签: php mysql codeigniter pagination


    【解决方案1】:

    你这里的说法有误,

    return $this->db->count_all("jobs");
    

    count_all("jobs") 将为您提供jobs 表中的行数。


    用这个替换它 -

    return $this->db->count_all_results("jobs");
    

    阅读文档 - http://ellislab.com/codeigniter/user-guide/database/active_record.html


    countci 的替代方法,

    $this->db->select('COUNT(*) as count');
    $this->db->where('`id` NOT IN (SELECT `jobs_id` FROM `jobs_user`)', NULL, FALSE);
    $query = $this->db->get('jobs');
    
    $result = $this->db->result($query);
    return $result->count;
    

    【讨论】:

    • 谢谢!这么小的东西!
    【解决方案2】:

    在您的实现中,您正在使用函数$this->db->count_all 计算表中的所有行。

    如果您想计算特定查询中的行数,您应该使用$this->db->count_all_results 函数。正确的实现是:

    function record_count() {
      $this->db->select('id');
      $this->db->from('jobs');
      $this->db->where('`id` NOT IN (SELECT `jobs_id` FROM `jobs_user`)', NULL, FALSE);
      return $this->db->count_all_results("jobs");
    }
    

    【讨论】:

    • 谢谢!也为提供文档的 Parag 提供了答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多