mysql - Using aggregate function in code igniter -


i have model transaction.php

<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class transaction extends ci_model{      public function  mytransaction(){         $this->load->database();         $query = $this->db->get('transaction');         return $query->result();     }      public function mysum(){         $this->db->select("sum(total) mysum");         $this->db->from("transaction");         return $query1->row();     } }  , controller invoice.php <?php if ( ! defined('basepath')) exit('no direct script access allowed');  class invoice extends ci_controller{     public function index(){         $this->load->model('transaction');         $data1['query'] =  $this->transaction->mytransaction();         $this->load->view('transactionview', $data1);     } } 

and lastly view

  <table width="600px" border="1px" style="margin:40px 300px;">         <tr>             <td>sno.</td>             <td>product code</td>             <td>name</td>             <td>quantity</td>             <td>rate</td>             <td>total</td>         </tr>       <?php foreach($query $row){?>     <tr>         <td><?php echo $row->transaction_id;?></td>         <td><?php echo $row->code;?></td>         <td><?php echo $row->name;?></td>         <td><?php echo $row->quantity;?></td>         <td><?php echo $row->rate;?></td>         <td><?php echo $row->total;?></td>     </tr>      <?php }?>      <tr>         <td colspan=4>subtotal</td>         <td colspan=2>?</td>     </tr>      <tr>         <td colspan=4>grand total</td>         <td colspan=2>?</td>     </tr>     </table> 

i trying use aggregate function codeigniter. want write query sums total column of table can echo grand total field of table.for how can pass sum value model view can echo sum value in table?

model function:-

public function mysum(){         $this->db->select("sum(total) mysum");         $this->db->from("transaction");         $query1 = $this->db->get();         if($query1->num_rows() > 0)         {           $res = $query1->row_array();          return $res['mysum'];         }        return 0.00;     } 

call model function in controller:-

 // set grand total here     $data1['g_total'] =  $this->transaction->mysum(); 

get in view as:-

    <tr>         <td colspan=4>grand total</td>         <td colspan=2><?php echo $g_total;?></td>     </tr> 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -