title: how to get sum of database column in codeigniter?
body: <p>Friends I'm Unable to get open positions Sum. here is my code. I am getting 1(one) instead of total sum. Help me to solve this issue.</p>
  
  <h3>Controller:</h3>
  
  <pre><code>function index(){
      $userId = $this-&gt;phpsession-&gt;get("userId");
      $userType = $this-&gt;phpsession-&gt;get('userType');
      $date = date("Y-m-d");
      if(!$userId &amp;&amp; !$this-&gt;phpsession-&gt;get("userType")){
          redirect(base_url().'user');
      }
  
      $config['base_url'] = base_url().'requirement/index';
      $config['total_rows'] = $this-&gt;requirement_model-&gt;GetRequirement(array("count"=&gt;true));
      $config['per_page'] = 5;
      $config['cur_tag_open'] = '&lt;a&gt;';
      $config['cur_tag_close'] = '&lt;/a&gt;';
  
      $this-&gt;pagination-&gt;initialize($config);
  
      $options['offset'] = $this-&gt;uri-&gt;segment(3);
      $options['limit'] = $config['per_page'];        
      $options['join']=true;
  
      $data['clients'] = $this-&gt;client_model-&gt;ajaxclient();
      $data['requirements'] = array(""=&gt;"Choose requirement");
  
      $data['requirement'] = $this-&gt;requirement_model-&gt;GetRequirement($options);
      $data['links'] = $this-&gt;pagination-&gt;create_links();
      $data['totalactive']=$this-&gt;requirement_model-&gt;GetRequirement(array("find"=&gt;true));
      $data['totalrequirement']=$this-&gt;requirement_model-&gt;GetRequirement(array("totalreq"=&gt;true));
      $data['openpositions']=$this-&gt;requirement_model-&gt;GetRequirement(array("openpos"=&gt;true));
      //print_R($data['openpositions']);exit;
      //echo "&lt;pre&gt;"; print_R($this-&gt;db-&gt;last_query()); exit;
  
      $data['page_title'] = "Requirement Details";
      $this-&gt;layout-&gt;view("requirement/index",$data); 
  
  }
  </code></pre>
  
  <p>This is my model function</p>
  
  <h3>Model:</h3>
  
  <p>function GetRequirement($options = array()){
          if(isset($options['requirementId']))
              $this->db->where('requirementId',$options['requirementId']);</p>
  
  <pre><code>    if(isset($options['clientName']))
          $this-&gt;db-&gt;where('clientName',$options['clientName']);  
  
      if(isset($options['limit']) &amp;&amp; isset($options['offset']))
          $this-&gt;db-&gt;limit($options['limit'], $options['offset']);
      else if(isset($options['limit']))
          $this-&gt;db-&gt;limit($options['limit']);
  
      $this-&gt;db-&gt;order_by("activateddate", "DESC");
  
      if(isset($options['join'])){
  
          $this-&gt;db-&gt;select('r.*,c.clientName as cName');
          $this-&gt;db-&gt;from('requirement as r');
          $this-&gt;db-&gt;join('clients as c','c.clientId=r.clientName');
          $query=$this-&gt;db-&gt;get();
          if(@$options['requirementId']) return $query-&gt;row(0);
          return $query-&gt;result();
      }
  
      if(isset($options['find'])){
          $this-&gt;db-&gt;select('distinct(clientName)');
          $this-&gt;db-&gt;from('requirement');
          $this-&gt;db-&gt;where('(clientName) and (noofpositions &gt; 0) ');
          $this-&gt;db-&gt;count_all();
          $query=$this-&gt;db-&gt;get();
          return $query-&gt;num_rows();  
      }
  
      if(isset($options['totalreq'])){
          $this-&gt;db-&gt;select('requirementName');
          $this-&gt;db-&gt;from('requirement');
          $this-&gt;db-&gt;where('(noofpositions &gt; 0) ');
          $this-&gt;db-&gt;count_all();
          $query=$this-&gt;db-&gt;get();
          return $query-&gt;num_rows();  
      }
  
  
      if(isset($options['openpos'])){
          $this-&gt;db-&gt;select_sum('openPos');   
          $this-&gt;db-&gt;from('requirement'); 
          $this-&gt;db-&gt;where('(closedPos = 0) ');   
          $this-&gt;db-&gt;count_all();
          $query=$this-&gt;db-&gt;get();
          return $query-&gt;num_rows();
      }
  
      $query = $this-&gt;db-&gt;get('requirement');
  
      if(isset($options['count'])) return $query-&gt;num_rows();
  
      if(@$options['requirementId']) return $query-&gt;row(0);
  
      return $query-&gt;result();
  }
  </code></pre>
  
  <p>This is my View page</p>
  
  <h3>View:</h3>
  
  <pre><code>&lt;div class="inner"&gt;
      &lt;h3&gt;&lt;?php echo $openpositions; ?&gt;&lt;/h3&gt;
       &lt;p&gt;Total Positions Opened&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>