CodeIgniter Forums
How to get values from single coloumn - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forum-10.html)
+--- Thread: How to get values from single coloumn (/thread-73492.html)



How to get values from single coloumn - ravhirzldi - 04-29-2019

Hello,


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:

Code:
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');
 
       $this->db->from('personaldata');
 
       $this->db->group_by('gender');

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


Thank you and sorry for my bad english   Angel


RE: How to get values from single coloumn - php_rocs - 04-30-2019

@ravhirzldi

Maybe these links will help:
https://codeigniter.com/user_guide/general/views.html (simply replace the array with the results from a model method)

https://www.guru99.com/codeigniter-mvc-framework.html


RE: How to get values from single coloumn - tp45 - 10-09-2019

Controller
PHP Code:
public function yourfunName(){
 
$data['gener_num'] = $this->yourModel->youModelFunction();

View

PHP Code:
<?php echo $gener_num?>   


regards


RE: How to get values from single coloumn - Wouter60 - 10-09-2019

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

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

View:
PHP Code:
<table>
  <tr><th>Gender</th><th>Total</th></tr>
  <?php foreach($stats as $row) : ?>
    <tr>
      <td><?= $row->gender;?></td>
      <td><?= $row->total;?></td>
    </tr>
  <?php endforeach;?>
</table> 

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