CodeIgniter Forums

Full Version: How to get values from single coloumn
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I have a table for personal data, which contains contents such as name, address, gender, etc.
now I want to calculate the total male or female from the gender column, so I make a sql query like this:

SELECT gender, COUNT(*) AS total
FROM personaldata
GROUP BY gender

the result is :

Quote:man : 3
woman : 6

So, how do I write the code on the model and controller and display it on my website?
this is my code so far:

PHP Code:
   public function total_gender()
       $this->db->select('gender, count(*) as total');

       $query $this->db->get()->row()->total;
       return $query->result();

Thank you and sorry for my bad english   Angel

Maybe these links will help: (simply replace the array with the results from a model method)
PHP Code:
public function yourfunName(){
$data['gener_num'] = $this->yourModel->youModelFunction();


PHP Code:
<?php echo $gener_num?>   

$query->result() returns an array of objects.

PHP Code:
$data['stats'] = $this->model-name->total_gender();

PHP Code:
  <?php foreach($stats as $row) : ?>
      <td><?= $row->gender;?></td>
      <td><?= $row->total;?></td>
  <?php endforeach;?>

Note: <?= is short for: <?php echo