SELECT * FROM abc
WHERE xyz LIKE $x OR xyz LIKE $y OR xyz LIKE $z
ORDER BY ((xyz LIKE $x) + (xyz LIKE $y) + (xyz LIKE $z)) DESC
I am able to write the query for pretty much ordey by asd desc
but how to write this complex query in codeigniter?
SELECT * FROM abc
WHERE xyz LIKE $x OR xyz LIKE $y OR xyz LIKE $z
ORDER BY ((xyz LIKE $x) + (xyz LIKE $y) + (xyz LIKE $z)) DESC
I am able to write the query for pretty much ordey by asd desc
but how to write this complex query in codeigniter?
You can write it in plain sql like you have it and execute it with the db->query
method.
$sql = 'SELECT * FROM abc
WHERE xyz LIKE ? OR xyz LIKE ? OR xyz LIKE ?
ORDER BY ((xyz LIKE ?) + (xyz LIKE ?) + (xyz LIKE ?)) DESC'
$this->db->query($sql, array($x, $y, $z, $x, $y, $z));
http://codeigniter.com/user_guide/database/queries.html query bindings explains the ?
Maybe you should check ActiveRecord:
$this->db->
from("abc")->
or_like("xyz",$x)->
or_like(...)->
order_by(...);
$result = $this->db->result_array();