Domanda

So I have a function in my model which pulls through jobs for a paginated table but only if they have not been booked.

My first function counts the results for how many pages there are etc.

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");
}

My second actually brings in the 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;
}

The problem is that the second function gets the jobs fine, and doesn't display the ones that are booked. But the first function which counts the pages still seems to count all the results, even the booked ones. Thus giving me empty pages and PHP invalid arguments on my paginated results.

I've messed around with a lot of ways, and would prefer active record if possible but I had no success with that.

Thanks,

È stato utile?

Soluzione

You have a wrong statement here,

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

count_all("jobs") will give you number of rows in jobs table.


Replace it with this -

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

Read documentation - http://ellislab.com/codeigniter/user-guide/database/active_record.html


OR an alternative method for count in ci,

$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;

Altri suggerimenti

In your implementation you are using function $this->db->count_all which counts all rows in a table.

If you want to count the number of rows in your particular query, you should use $this->db->count_all_results function. Correct implementation is:

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");
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top