CodeIgniter COUNT with active record?


I am working in codeigniter.. I want to count the no. of rows having the same order_no.. below is my code.

public function get_my_orders() { $user_data = $this->session->all_userdata(); $email = $user_data['user_email']; $this->db->select('*'); $this->db->from('order_details'); $this->db->where('email', $email); $this->db->group_by('order_no'); $this->db->order_by('order_no', 'DESC'); $query = $this->db->get(); return $query->result(); }

Pls help..


Try changing your select line to look like this:

$this->db->select('COUNT(*) as count');

Rather than having all fields accessible like they currently are, you will instead only have access to one variable called count. To keep all variables accessible and add the count in as well, use this instead:

$this->db->select('*, COUNT(*) as count');

Feel free to change the lowercase name for count, I'm just using that as an example.


You can use $this->db->count_all_results()

public function get_my_orders() { $user_data = $this->session->all_userdata(); $email = $user_data['user_email']; //$this->db->select('*');// no need select as you only want counts $this->db->from('order_details'); $this->db->where('email', $email); //$this->db->group_by('order_no');//no need group_by as you only want counts //$this->db->order_by('order_no', 'DESC');//no need order_by as you only want counts return $this->db->count_all_results();; }

Check Codeigniter Manual

$query = $this->db->get(); return $query->num_rows(); //Return total no. of rows here <blockquote>

$query->num_rows(); will count the total active record return by your query.




  • Fiddler makes web browsing very slow
  • finding the optimized location for a list of coordinates x and y
  • comparing columns in two separate pandas dataframes
  • Elm - producing a list of random number changing with time
  • How should I use JpaRepository.findOne() with SpringBoot?
  • How to set the default push/pull repository from TortoiseHG
  • Got Exception Error “Exception in thread Thread-1 (most likely raised during interpreter shutdown)”
  • How to pass object in nested functions?
  • How to avoid bootstrap css overriding the custom css?
  • JSF Datatable link to another page
  • Delete export variable and help removing circular dependency
  • ASP.NET grid in 2.0 having trouble with paging to retain value of check box on save button click
  • Integrating Yelp API v2 into iOS 7 app
  • Getting an error serving images from App_Themes when using precompilation?
  • How to hide 'Add To Cart' for variable products, but keep product variations visible
  • Limit # of records returned based on a form control
  • How to use magento api with iOS app
  • Insert statement not working using execute(array()) of PDO Extension
  • Making query to find nearest multiple(Lat,Long) from the single(Lat,Long)
  • the IBM_JAVA error for running jobs in Hadoop 2.2.0
  • iOS App crash issue `[UIWindow warpPoint:]`
  • Slick: How can I combine a SQL LIKE statement with a SQL IN statement
  • Unable to connect to AWS RDS through PDO
  • Spotify cocoalibspotify offline status set to 1 but all tracks stuck at waiting
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • Regex not working in java 1.5
  • Runtime complexity of getting the length of a string in different representations
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • Using redis as an LRU cache for postgres
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • How to get rgb from transparent pixel in js
  • Computing the discrete fourier transform of audio data with FFTW