0

This is my controller:

public function index()
{
    $data['page_title'] = 'Home';
    $data['new_grievances'] = $this->admin_home->get_members();
    $data['num_row'] = count($data['new_grievances'] );
    print_r($data['new_grievances']);
    print_r($data['num_row']);
    $this->load->view('admin/grievances_admin_home' , $data);
}

This is my model:

function get_members() {
    // $query = $this->db->get('enquiry');
    // return $query;
    $this->db->from('enquiry a');
    $this->db->join('enquiry_department b', 'a.department=b.id' , 'inner');
    $this->db->join('enquiry_user_type c', 'a.user_type=c.id' , 'inner');
    $this->db->WHERE('a.status IS NULL');
    $query = $this->db->get(); 
    return $query;
}

I want to get the rows and and count of the number of rows.

Here, I execute the query $data['new_grievances'] number of rows is 0 and so output of $data['num_row'] must return zero.

When I have a result with at least one row, then I print the $data['new_grievances'] value it gives:

CI_DB_mysqli_result Object (
    [conn_id] => mysqli Object (
        [affected_rows] => 0
        [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $ 
        [client_version] => 50011
        [connect_errno] => 0
        [connect_error] =>
        [errno] => 0
        [error] =>
        [error_list] => Array ( )
        [field_count] => 12
        [host_info]
        ...

How do I get the rows in the result set?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
rangaraj seo
  • 31
  • 2
  • 8

5 Answers5

3

Change your code from return $query; to return $query->result(); by this you can also use your $data inside the view also and count the total results in your controller.

This is controller

public function index()
{
    $data['page_title'] = 'Home';
    $data['new_grievances'] = $this->admin_home->get_members();
    $data['num_row'] = count($data['new_grievances'] );
    print_r($data['new_grievances']);
    print_r($data['num_row']);
    $this->load->view('admin/grievances_admin_home' , $data);
}

This is my model

function get_members() {
    $this->db->from('enquiry a');
    $this->db->join('enquiry_department b', 'a.department=b.id' , 'inner');
    $this->db->join('enquiry_user_type c', 'a.user_type=c.id' , 'inner');
    $this->db->WHERE('a.status IS NULL');
    $query = $this->db->get(); 
    return $query->result();
}
Riyenz
  • 2,498
  • 2
  • 9
  • 23
  • If there is no row in the database then you will get error Message: count(): Parameter must be an array or an object that implements Countable – user9437856 Sep 22 '18 at 06:47
  • @user9437856 then you have done something differently/incorrectly. Codeigniter's `result()` method unconditionally returns an array. If there are no rows in the result set, then it returns an empty array. If there are rows in the result set, then it will return an array of objects. – mickmackusa Nov 20 '20 at 05:28
1

Please follow below lines.

public function index()
{
    $data['page_title'] = 'Home';
    $new_grievances = $this->admin_home->get_members();
    $data['num_row'] = $new_grievances->num_rows();
    print_r($new_grievances->result());
    $data['new_grievances'] = $new_grievances->result()
    print_r($data['num_row']);
    $this->load->view('admin/grievances_admin_home' , $data);
}
Ayyappa amara
  • 437
  • 3
  • 16
0

Simply use the below line to get your results from DB

$query = $this->db->get(); $results = $query->result();

and use the below code to get number of rows found $count = $query->num_rows();

Jawad A.
  • 73
  • 6
0
public function index()
{
    $data['page_title'] = 'Home';
    $data['new_grievances'] = $this->admin_home->get_members();
    /*Old Code
    $data['num_row'] = count($data['new_grievances'] );*/
    //New Code
    $data['num_row'] = $data['new_grievances'];
    print_r($data['new_grievances']);
    print_r($data['num_row']);
    $this->load->view('admin/grievances_admin_home' , $data);
}

Model

function get_members() {
    $this->db->from('enquiry a');
    $this->db->join('enquiry_department b', 'a.department=b.id' , 'inner');
    $this->db->join('enquiry_user_type c', 'a.user_type=c.id' , 'inner');
    $this->db->WHERE('a.status IS NULL');
    return $this->db->count_all_results();
}
Jaimin Vyas
  • 114
  • 5
  • The OP clearly stated: `i want to get the rows and and count of the number of rows.` This means that your unexplained answer does not deliver the required data and is therefore incorrect and should be removed. – mickmackusa Nov 20 '20 at 05:30
0

As I said in this similar answer, as long as you are only using the row count in one layer of your MVC structure, there is no real gain in declaring a count variable.

  • If you are only going to use the value in your View, then only call count() in your View.
  • If you are only going to use the value in your Controller, then only call count() in your Controller.
  • If you need the row count in both the Controller AND the View, then write D.R.Y. code and cache the row count as a variable in the Controller, then pass it to the View.

Ultimately, I'm recommending that you not bloat the variable scope with data that is instantly accessible with a O(1) function call.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136