My solutions is that: To create view which will contains all results ordered, before select this view in codeigniter model
Example:
select (case when (`parajurist`.`intrebari`.`Status` = 2) then 1 else 2 end) AS `Second`,`parajurist`.`intrebari`.`IdIntrebare` AS `IdIntrebare`,`parajurist`.`intrebari`.`Titlu` AS `Titlu`,`parajurist`.`intrebari`.`Descriere` AS `Descriere`,`parajurist`.`intrebari`.`NumePrenumeP` AS `NumePrenumeP`,`parajurist`.`intrebari`.`EmailP` AS `EmailP`,`parajurist`.`intrebari`.`Status` AS `Status`,`parajurist`.`intrebari`.`IdCategorie` AS `IdCategorie`,`parajurist`.`intrebari`.`DataAdresare` AS `DataAdresare`,`parajurist`.`intrebari`.`Comments` AS `Comments`,`parajurist`.`intrebari`.`CuvCheie` AS `CuvCheie` from (`parajurist`.`intrebari` join `parajurist`.`intrebaricategorii` on((`parajurist`.`intrebaricategorii`.`IdCategorie` = `parajurist`.`intrebari`.`IdCategorie`))) where (`parajurist`.`intrebari`.`Status` <> 0) order by (case when (`parajurist`.`intrebari`.`Status` = 2) then 1 else 2 end),`parajurist`.`intrebari`.`IdIntrebare` desc
and codeigniter code:
$this->db->limit($start, $stop); $this->db->select('*'); $this->db->select('LEFT(intrebari_view.Titlu, 50) as Titlu'); $this->db->select('LEFT(intrebari_view.Descriere, 150) AS Descriere'); $this->db->join('IntrebariCategorii', 'IntrebariCategorii.IdCategorie = intrebari_view.IdCategorie'); $this->db->where('IntrebariCategorii.NumeCategorie', $cat); $this->db->from('intrebari_view'); $query = $this->db->get();
CASE.