Tristan
Tristan

Reputation: 71

How to display total in view page (Codeigniter)

I am just new to Codeigniter. I want to display the total sum of bill_rpt of this SQL query in the view page. here's the code

this is my Model

public function total() {
    $sql="SELECT bill_rpt
    FROM amilyar_bill
    WHERE bill_status = 1";
    return $this->db->query($sql);
}

this is my Controller

public function payments($token=''){
    $this->isLoggedIn();
    $data = array(
            // get data using email
        'token' => $token,
        'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),

        'notifications' =>  $this->model->notification_admin($this->session->userdata('email'))->result_array(),
        'notification' =>  $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
        'total' =>  $this->model->total($this->session->userdata('email'))->result_array(),

    );

    if ($this->session->userdata('position_id') == 2) { 
        $this->load->view('includes/admin_header',$data);
        $this->load->view('admin/reports/payments',$data);
    } else {
        $this->logout();
    }
}

this is my View

<h4 class="pull-right">Total:</h4>

Thanks in advance

Upvotes: 1

Views: 1713

Answers (3)

Pradeep
Pradeep

Reputation: 9717

Hope this will help you :

Use ci select_sum() to get the sum

Your total method should be like this :

public function total()
{
   $this->db->select_sum('bill_rpt');
   $this->db->where('bill_status' , '1');
   $query =  $this->db->get('amilyar_bill');
   return $query->row()->bill_rpt;
}

Your $data part in controller payments method should be like this :

$data = array(
  'token' => $token,
  'admin_info' => $this->model->getAdminInfo($this->session->userdata('email'))->row(),
  'notifications' =>  $this->model->notification_admin($this->session->userdata('email'))->result_array(),
  'notification' =>  $this->model->all_notification_admin($this->session->userdata('email'))->result_array(),
   'total' =>  $this->model->total()
);

In payments view part use $total like this :

<h4 class="pull-right">Total: <?=$total;?></h4>

Upvotes: 1

Gufran Hasan
Gufran Hasan

Reputation: 9373

Try SUM function of MySql as:

In Model

public function total(){
$sql="SELECT SUM(bill_rpt ) as total_sum
FROM amilyar_bill
WHERE bill_status = 1";
return $this->db->query($sql);
}

Upvotes: 0

Pupil
Pupil

Reputation: 23958

You can use num_rows() method of Active Class.

$query->num_rows();

public function total(){
 $sql="SELECT bill_rpt FROM amilyar_bill WHERE bill_status = 1";
 $query = $this->db->query($sql);
 return $query->num_rows();
}

And in View,

<h4 class="pull-right">Total: <?php echo $total;?></h4>

Upvotes: 0

Related Questions