0
votes

transaction table
Title:
001. PHP CookBook
002. C++ Beginner
003. Advanced Java
004. PHP CookBook
005. PHP CookBook
006. Advanced Java

What I want to create is a list of the most purchased book from the Transaction table as shown above and generate a list like this:

Most purchased:

  1. PHP CookBook
  2. Advanced Java
  3. C++ Beginner

I work using PHP CI and what I've done is like this:

$this->db->select('Title');
$this->db->from('transaction');
$this->db->count('Title AS TitleCount');
$this->db->group_by('AssetTitle');
$this->db->order_by('TitleCount','ASC');
$this->db->limit(4);
$temp = $this->db->get();
$temp->num_rows();
return $temp->result_array();

But the result wasn't like as I wanted. The COUNT is not recognized by CodeIgniter.
I don't quite understand about query, so maybe if there someone out there who can help me with this query, would be greatly appreciated!

1
Did you try: $this->db->order_by('count(*)','ASC'); - Gordon Linoff
It works! Only need to replace the ASC into DESC. Thanks! @GordonLinoff - RainBi

1 Answers

1
votes
$this->db->select('Title,count(Title) AS TitleCount',FALSE);
$this->db->from('transaction');
$this->db->group_by('AssetTitle');
$this->db->order_by('TitleCount','ASC');
$this->db->limit(4);

try this