⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to show the count from some other table on the Listing paage



karan
  • profile picture
  • Member

Posted 27 June 2013 - 10:20 AM

I want to show the count of a column from some other table on the Listing page.


davidoster
  • profile picture
  • Member

Posted 27 June 2013 - 12:15 PM

Hello and welcome to the forums [member=karan].

Which theme do you use?


davidoster
  • profile picture
  • Member

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?


karan
  • profile picture
  • Member

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.


davidoster
  • profile picture
  • Member

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!