title: How to calculate sum of unique column (column name is total) using codeigniter
body: <p><strong>Controller</strong></p>
  
  <p>This is Controller Page.</p>
  
  <pre><code>public function index() {
  $data = array(
       'year' =&gt; $this-&gt;year_model-&gt;list_all(),
       'speces' =&gt; $this-&gt;speces_model-&gt;list_all(),
         );
            foreach ($data['year'] as $value):
  
                  $temp = array();
                  $temp['year_key'] = $value['year-range'];
                  $temp['year_id'] = $value['id'];
  
  
                  $temp['speces_key'] = array();
                  foreach ($data['speces'] as $value1):
  
                      $temp['speces_key'][$value1['name']] = $this-&gt;db-&gt;get_where('expendituredata', array('spacesId' =&gt; $value1['id'], 'yearId' =&gt; $value['id']))-&gt;row_array();
  
                  endforeach;
                  $final_result[] = $temp;
  
              endforeach;
              $data['result'] = $final_result;
              $this-&gt;load-&gt;view('site/template', $data);
          }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>public function list_all() {
          return $this-&gt;db-&gt;get('year')-&gt;result_array();
      }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <p>This is View file.</p>
  
  <pre><code>&lt;table&gt;
   &lt;thead&gt;
         &lt;tr&gt;
            &lt;th&gt;Year Range&lt;/th&gt;
            &lt;?php foreach ($speces as $value): ?&gt;
              &lt;th&gt; &lt;?php echo $value['name']; ?&gt;&lt;/th&gt;
             &lt;?php endforeach; ?&gt;
              &lt;th&gt;TOTAL&lt;/th&gt;
               &lt;/tr&gt;
               &lt;/thead&gt;
           &lt;tbody&gt;
                  &lt;?php foreach ($result as $value): ?&gt;
                    &lt;?php $sum = 0; ?&gt;
                      &lt;tr&gt;
                       &lt;td&gt;&lt;?php echo $value['year_key']; ?&gt;&lt;/td&gt;
                  &lt;?php foreach ($value['speces_key'] as $key =&gt; $value2): ?&gt;
                    &lt;td&gt;&lt;?php echo $value2['data']; ?&gt;&lt;/td&gt;
                       &lt;?php $sum+= $value2['data']; ?&gt;
                          &lt;?php endforeach; ?&gt;
  
                      &lt;td&gt;&lt;?php echo $sum; ?&gt;&lt;/td&gt;
                     &lt;/tr&gt;
            &lt;?php endforeach; ?&gt;
  
              &lt;tr&gt;
                 &lt;td&gt;Total&lt;/td&gt;
                    &lt;?php foreach ($speces as $value6): ?&gt;
    &lt;?php
    $this-&gt;db-&gt;select_sum('data');
    $query = $this-&gt;db-&gt;get_where('expendituredata', array('spacesId' =&gt; $value6['id']))-&gt;row_array();
  
   $totals = $query['data'];                                      
       ?&gt;
        &lt;td&gt;&lt;?php echo $totals; ?&gt;&lt;/td&gt;    
        &lt;?php endforeach; ?&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>[<a href="http://i.stack.imgur.com/OI1i0.png" rel="nofollow">Screen Shot</a>]</p>
  
  <p>My Question: How to calculate sum of column (column name is total).column name total is display above screenshot.so how to calculate sum of column(column name is total) and sum display in red arrow in above screen shot</p>