RainBi
RainBi

Reputation: 31

Query in CodeIgniter for most purchased data

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!

Upvotes: 0

Views: 65

Answers (1)

Bhadra
Bhadra

Reputation: 2104

$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

Upvotes: 1

Related Questions