I want to show the count of a column from some other table on the Listing page.
How to show the count from some other table on the Listing paage
- Single Page
Posted 27 June 2013 - 10:20 AM
Posted 27 June 2013 - 12:15 PM
Hello and welcome to the forums [member=karan].
Which theme do you use?
Posted 27 June 2013 - 12:30 PM
Oh hold on,
so for example you set_table('customers'); but you want to SELECT COUNT(`actor_id`) FROM `actor`
and display the result on the listing of the table of customers. Right?
Is there something tricky to this?
Posted 28 June 2013 - 06:06 AM
Oh hold on,
so for example you set_table('customers'); but you want to SELECT COUNT(`actor_id`) FROM `actor`
and display the result on the listing of the table of customers. Right?
Is there something tricky to this?
Yes, exactly I want to have the same functionality.
Posted 28 June 2013 - 06:44 AM
Ok, then
a. make a new model under application/models, e.g. mymodel.php
(you don't necessarily need it but it is a good practise)
b. include this code
<?php class mymodel extends CI_Model { function __construct() { parent::__construct(); $this->load->database(); } public function my_count($table, $column) { $query = "SELECT COUNT(`" . $column . "`) AS `" . $column . "` FROM `" . $table . "`"; $result = $this->db->query($query); return $result; } }
and then call it within your controller,
... $this->load->model('mymodel'); $result = $this->mymodel->my_count('table_name', 'column_name'); $row = $result->row(); // $row->column_name holds the count!