phpactiverecordsql-order-bycodeigniter-3custom-sort

How to use CodeIgniter's active record to sort records by day of the week name


I have a database column "days". After 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 using CodeIgniter active record?


Solution

  • 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'));