1

I am looking for a solution to display number of rows based on a particular column data using CodeIgniter.

This is basically for a Shop registration project. I have a database table named shopowner with a column named status. and this status column have only 3 values. ie. if status is 0 then the record is waiting for approval, once approved the status will become 1 and 2 for rejection. Now in my Dashboard I need to display total number of pending, Approved and Rejected records. Can any one help me out with this and please tell me what to add in Model, Controller and view. I am totally new to CodeIgniter. Thanks in advance.

Model

public function count_rows($status)
    {
        $this->db->select('status, COUNT(status) as total');
        $this->db->group_by('status', $status);  
        $this->db->get('shopowner');
    }

Expected output would be like

Total Registration : 4 Pending Approval : 2 Approved : 1 Rejected : 1

1
  • its look right to me just return the value return $this->db->get('showowner')->result(); Commented May 21, 2019 at 10:49

3 Answers 3

0

If you want to get all statistics in one query, u can use following code

 $this->db->select('status, COUNT(status) as total');
 $this->db->group_by('status');  
 $this->db->get('shopowner');

But if u want to get a spesific status count, u have to add your status code into where parametr

$this->db->where(['status' => $status]);
$result = $this->db->get('shopowner');
echo $result->num_rows();

I hope this will help u)

private function count_rows($status) {
    $this->db->where(['status' => $status]);
    return $this->db->get('shopowner')->num_rows();
}

In your controller

    $data = [
        'pending_approval' => $this->count_rows(4),
        'approved' => $this->count_rows(2),
        'rejected' => $this->count_rows(1)
    ];
    $this->load->view('your_view', $data);

You can use <?=$approved?> to call $data items

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Farhad for the your help. If I choose the second option here how can I pass this parameter to controller then view it as Total count when status=0 | status=1 | status=2
0

There no need to give 'status' column at first line of code, only use '$this->db->select('COUNT(*) as total');' at first line and use where clause with status column instead of 'status' column using in 'group_by' clause. Can you try following code:

public function count_rows($status)
{
    $this->db->select('COUNT(*) as total');
    $this->db->from('shopowner');
    $this->db->where('status', $status);
    $this->db->group_by('status');  
    $this->db->get();
}

Comments

0

I have made a database table named shopowner with 4 columns named: id, name, status, type_name. The status column has only 3 types of values which is like you described. If status is 0 then the record is waiting for approval, once approved the status will become 1 and 2 for rejection. type_name shows reject approved and waiting in it, and type_name have name type like reject approved or waiting, id is autogenerated, and name is customer name.

model

$this->db->select('type_name, COUNT(status) as star ');
$this->db->from('shopowner');
$this->db->group_by('status');

controller

$data['user']=$this->your_model->name();
$this->load->view('your_view', $data);

view

<table>
  <tr>
<th>name</th>
<th>total</th>
</tr>
    <?php foreach( $user as $users ){
        ?>
     <tr>
    <td><?php echo $users->type_name?></td>
    <td><?php echo $users->star?></td>
    </tr>
    <?php } ?>
  </table>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.