zazvorniki
zazvorniki

Reputation: 3602

CodeIgniter - sql return only the first letter

I'm working on a directory for a vocabulary page for a site I'm building. So when you click the A link it will pull back all the letters that start with A and so on. I have most of the functionality done, but I can't get the right sql statement to pull back what I want.

I'm working on this...

$this->db->order_by("title", "asc");
$this->db->where('category', 'vocab');
$this->db->like('title', $this->uri->segment(3));
$data['query'] = $this->db->get('entries');

So, I've been trying to get the like function working. To match the first letter in the title with the letter I'm throwing in the url when the user clicks on the link.

Any help? Am I doing the like clause right? I've never actually had to do one before.

Upvotes: 2

Views: 2451

Answers (2)

Syed Amir Bukhari
Syed Amir Bukhari

Reputation: 136

If you want to control where the wildcard (%) is placed, you can use an optional third argument. Your options are 'before', 'after' and 'both' (which is the default).

$this->db->like('title', 'match', 'before'); // Produces: WHERE title LIKE '%match'
$this->db->like('title', 'match', 'after'); // Produces: WHERE title LIKE 'match%' 
$this->db->like('title', 'match', 'both'); // Produces: WHERE title LIKE '%match%'

You can see the codeIgniter Active Record Page

Upvotes: 0

Shomz
Shomz

Reputation: 37701

I believe you need to change one row:

$this->db->like('title', $this->uri->segment(3), 'after'); 

Also, note the way you get results: $this->db->get('entries')->result();

Upvotes: 6

Related Questions