M-Fulu
M-Fulu

Reputation: 91

PHP SUM total in the database

Hi I am new in PHP but I want to Sum my amount and display it on the page currently I am doing like this but it is not showing.

 <div class="col-md-4 col-lg-4">
            <div class="panel panel-bordered panel-black">
                <div class="panel-heading">
     <h3 class="panel-title"><?php echo translate('Total Amount ');?></h3>
                 </div>
                <div class="panel-body">
                    <div class="text-center">
                        <h1>
<?php 
 echo $this->db->select('SUM(amount) as Total');
 $getData =  $this->db->get_where('tablename',array('amount = '!=0))->first_row();
 echo $getData->Total; 
 ?>
                        </h1>
                    </div>
                </div>
            </div>
        </div>

Upvotes: 0

Views: 383

Answers (2)

M-Fulu
M-Fulu

Reputation: 91

 <div class="col-md-4 col-lg-4">
            <div class="panel panel-bordered panel-black">
                <div class="panel-heading">
     <h3 class="panel-title"><?php echo translate('Total Amount ');?></h3>
                 </div>
                <div class="panel-body">
                    <div class="text-center">
                        <h1>
<?php 
 echo $this->db->select('SUM(amount) as Total');
 $getData =  $this->db->get_where('tablename',array('amount = '!=0))->first_row();
 echo $getData->Total; 
 ?>
                        </h1>
                    </div>
                </div>
            </div>
        </div>

Upvotes: 0

Bhargav Chudasama
Bhargav Chudasama

Reputation: 7161

try this

<?php
$data = $this->db->get_where('tablename',array('amount = '=>1))->sum(amount)->result();
foreach($data as $row): 
echo $row['amount'];  //if array output
echo $row->amount; //object output
endforeach;
?>

Upvotes: 1

Related Questions