数据库中的PHP SUM总和

时间:2018-07-23 05:33:06

标签: php mysql

嗨,我是PHP新手,但我想总结一下自己的金额并将其显示在当前我正在这样做的页面上,但未显示。

 <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>

2 个答案:

答案 0 :(得分:1)

尝试

<?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;
?>

答案 1 :(得分:0)

 <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>