![]() |
Custom Sort Query Syntax - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6) +--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17) +--- Thread: Custom Sort Query Syntax (/showthread.php?tid=82131) |
Custom Sort Query Syntax - imranp - 06-15-2022 Hey guys, I'm hoping you can help me figure out the correct syntax for a query. PHP Code: $main_cat = $this->db->from('categories') PHP Code: ( By looping through the array, I am extracting the [name] for each member of the array: PHP Code: foreach ($main_cat as $key_main_cat => $values_main_cat) Which produces the following result: Beverages Condiments Canned Fruits, Vegetables, & Meats Household Soups, Broth, & Gravies Pastas, Rice & Grains Breakfast Kitchen Paper Products Beans & Chili Baking & Cooking Baby Products Health & Beauty C-Store Supplies Snacks I would like to sort this list in a custom order. I can think of two ways of doing this, but can't figure out the syntax for either method. Method 1: Use Order_By condition in the query I tried: PHP Code: ->order_by("id", "8,4,11,35,27,44,72,50,19,33,51,52,80,12,30") but it didn't work. What would be the correct syntax? Method 2: Sort the $main_cat array after the data has been populated No idea how to do this.... probably simpler than method 1, but don't know how to sort the array using the custom order. I appreciate your time and assistance!! |