1

Using Codeigniter 3, I would like to display all the records from a table in a MySQL database. I'd also like to include the number of records selected.

For example;

Showing x number of records;

record 1
record 2
record 3
etc

Currently I have the following (which works);

// select all records
public function selectRecords() {
    $this->db->select('*');
    $this->db->from('records');
    $query = $this->db->get();
    return $query->result_array();
}

// count all records 
public function countRecords() {
    $this->db->select('count(*) as count');
    $this->db->from('records');
    $query = $this->db->get();
    return $query->row();
}

My question is do I need two separate queries in order to achieve this (select and count)?

Is there a more efficient way of achieving what I want?

1
  • You can display count as count(array) in view. No need to do any particular query for this. Commented May 10, 2018 at 13:22

5 Answers 5

1

You can do something like this :

public function selectRecords() 
{
    $query = $this->db->get('records');
    if ($query->num_rows() > 0 )
    {
       $records = $query->result_array();
       $data['count'] = count($records);
       $data['all_records'] = $records;
       return $data;
    }  
}

Pass it to the view from your controller :

 $data = $this->model_name->selectRecords();
 /*print_r($data) to see the output*/
 $this->load->view('your_view',$data);

In view :

<?php echo $count .' number of records';?>
Sign up to request clarification or add additional context in comments.

1 Comment

I do not endorse this model method design -- it is only conditionally returning a payload. If there are no rows in the result set, there is no return. This will mean that the $count and $all_records will not exist in the view and will emit complaints from PHP.
1

you can do only:

public function selectRecords() {
    $this->db->select('*');
    $this->db->from('records');
    $query = $this->db->get();
    return $query->result_array();
}

and

$records = $this->selectRecords();
$count = count($records);

1 Comment

select('*') never needs to be added to an active record chain. get() can receive the tablename, therefore the from() method call can also be removed. Simply write: public function select(): array { return $this->db->get('records')->result_array(); } Then the call from the controller would be $records = $this->Records_model->select();
1

In The first function itself you can get the count using $query->num_rows() function

public function selectRecords() {
   $return = array();
   $this->db->select('*');
   $this->db->from('records');
   $query = $this->db->get();
   $return['count']   =  $query->num_rows(); 
   $return['records'] =  $query->result_array();
   return $return;
} 

1 Comment

select('*') never needs to be added a CodeIgniter active record -- it is the default. get()'s first parameter is the tablename, so the from() call can be omitted too. Use type hinting everywhere possible. public function selectRecords(): array { $resultObject = $this->db->get('records'); return ['count' => $resultObject->num_rows(), 'records' => $resultObject->result_array()]; }
1

try this it will help you to provide pagination for records

public function selectRecords($params = array(), $count = false) {

    $offset = isset($params['offset']) ? $params['offset'] : '';
    $limit = isset($params['limit']) ? $params['limit'] : '';
    $this->db->select('*');
    $this->db->from('records');

    $query = $this->db->get();
    if ($count) {
           return $this->db->get()->num_rows();
      }

      if (empty($offset) && !empty($limit)) {
           $this->db->limit($limit);
      }
      if (!empty($offset) && !empty($limit)) {
           $this->db->limit($limit, $offset);
      }

      $result = $this->db->get()->result();
      return $result;
}

1 Comment

This question never mentioned pagination. select('*') never needs to be written in CodeIgniter. If $params might hold limit and offset data then that can be null coalesced directly into the get() call after the tablename is passed in. I'd say that a selectRecords() method should never return the num rows count -- that would be a poor naming choice. A method to return an array or an integer is going to be messy. I would write only this: select(?int $limit = null, ?int $offset = null): array { return $this->db->get('records', $limit, $offset)->result(); }. See? No mess.
0

Your model method only needs to return the 2d payload to the controller (it doesn't need to be tasked with also counting the rows).

public function getAll(): array
{
    return $this->db->get('records')->result_array();
}

In your controller, you can just call count() if you need it at that level. If the count is only needed by the view, then only call count() in the view.


If there are times when you only want the count of all rows and don't need the actual row data (in a single server request), then yes, it is sensible to dedicate a method to this singular responsibility (but I've never found myself in this situation).

public function countAll(): int
{
    return $this->db->count_all('records');
}    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.