Welcome Guest, Not a member yet? Register   Sign In
string based custom ordering in active record
#1

[eluser]tsandesh23[/eluser]
This is my normal mysql query:
Code:
$sql = "SELECT * FROM item order by ";
if(my_condition)
{
       $sql. = "FIELD(`category`, 'Auto & Accessories', 'Couch', 'Bed',
       'Desk &     Office', 'Bike &            Scooter', 'Tools', 'Leisure',
        'Iron & Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator & Appliances',
        'Toys & Games', 'Chair', 'Table', 'Garden & Terrace', 'TV, HIFI & Computers',
        'General Item')";

}
else
{
     $sql .= "category asc";
}

I need it in CI in active record. I tried in following way:
Code:
if(my_condition)
            {
                $this->db->order_by("FIELD(`category`, 'Auto & Accessories', 'Couch',
                'Bed', 'Desk & Office', 'Bike & Scooter', 'Tools', 'Leisure', 'Iron &
                 Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator &
                 Appliances', 'Toys & Games', 'Chair', 'Table', 'Garden & Terrace',
                 'TV, HIFI & Computers', 'General Item')");

            }
            else
            {
                $this->db->order_by("category", "asc");
            }
But, I got this error::

Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'Couch'`, `'Bed'`, `'Desk` &
Office', `'Bike` & Scooter', `'Tools'`, `'Leisure'`,' at line 6


Filename: C:\xampp\htdocs\straatjutter_service\system\database\DB_driver.php

Line Number: 330
How to solve it? Actually I need my item's category sorted in above format. This can be done in normal mysql query. But, get confused in doing it in active records.


Messages In This Thread
string based custom ordering in active record - by El Forum - 03-23-2013, 04:55 AM
string based custom ordering in active record - by El Forum - 03-23-2013, 06:33 AM
string based custom ordering in active record - by El Forum - 03-23-2013, 09:35 AM
string based custom ordering in active record - by El Forum - 03-23-2013, 10:02 AM
string based custom ordering in active record - by El Forum - 03-23-2013, 08:15 PM
string based custom ordering in active record - by El Forum - 03-24-2013, 04:11 AM
string based custom ordering in active record - by El Forum - 03-24-2013, 05:09 AM
string based custom ordering in active record - by El Forum - 03-24-2013, 07:09 AM
string based custom ordering in active record - by El Forum - 03-27-2013, 03:04 AM



Theme © iAndrew 2016 - Forum software by © MyBB