one_loop one_loop - 24 days ago 6
PHP Question

Codeigneter : How to use Order by in Active record?

I have a database column

"days"
. I would like to insert the days of the week randomly example
Tuesday,Monday,Thursday,Wednesday


| id | Days
-------------------
| 1 | Tuesday
| 2 | Monday
| 3 | Thursday
| 4 | Wednesday


How would I return the days in order to my view using Codeigniter Active Record ?

Answer

If the column Days only contains the day of week, then you could do this:

$this->db->query(SELECT * FROM TABLENAME ORDER BY FIELD(Days, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY'));