CodeIgniter Order By Query: In SQL queries, ORDER BY clause/keyword provides a way to sort the resultset in ascending or descending order based upon columns/fields. The resultset can be sorted using one or more columns too. Most of the database systems sort the records in ascending order by default if not specified. Here we'll see how to write order by query in codeigniter framework to sort the database records.
To sort query resultset in codeigniter you have to use order_by() method from 'Active Record Class' library. The method takes up two parameters, first one is the table column name and second one is optional and specifies if the column values should be sorted in ascending or decending order.
order_by(column_name, sort_type);
CodeIgniter Order By Query Example:
Here let's see how to write order by query in codeigniter using order_by()
function. Below is a sample db table we'll be using for writing order_by queries.
MySQL Table: Books
![Table - Books order-by-query-in-codeigniter-table-books](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjm2W90aBS82ZNE09NVhKa8A2N1qojl_aSkC3GVsxnjVuERyV1pmCa5P2ESEfmnMtMnCpFXPjAziUt9uR0oG-9OGWc4VU2eg72NaVi_8C6q70hRgyrXu5iB1KwsrjBdOHdZ9WmALdboIflY/s1600/order-by-query-in-codeigniter-table-books.png)
Below is a simple order_by query in codeigniter which pulls off the data from the table 'Books' and sort the result set by 'BookName' field in alphabetical order. Also note that the query doesn't specify if 'BookName' should be sorted in ascending or descending order. But by default MySQL DB sorts data in ascending order so the column will be sorted alphabetically.
$this->db->select('*'); $this->db->from('tbl_Books'); $this->db->order_by('BookName'); $query = $this->db->get(); return $query->result(); // Produces SQL // SELECT * FROM tbl_Books ORDER BY BookName;
Resultset:
![CodeIgniter Order By Query Example codeigniter-order-by-query-example](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHtDkZm8IP2HKFqB09zhAJpaQifnZs_iLiyzDdGLAp6rP4vXd-j0quHyD5iebpl4GXpkL4cZ7i-9rP-RXcCgrmTZ1_OL96SJe5WRe75CurOw9HYx3EjNe_1CU2PCvy4KLy6XLQNeEx0upS/s1600/codeigniter-order-by-query-example.png)
You can skip the statement $this->db->select('*');
unless you want to select only specific columns/fields.
The above query can be shortened like this in codeignitar,
$this->db->order_by('BookName'); $query = $this->db->get('tbl_Books'); return $query->result(); // Produces SQL // SELECT * FROM tbl_Books ORDER BY BookName;
CodeIgniter Order By Asc:
This codeigniter order by query explicitly specifies to sort the resultset using 'Author' column in ascending order. To sort a column by ascending order use asc
as second parameter in order_by() method.
$this->db->select('*'); $this->db->from('tbl_Books'); $this->db->order_by('Author', 'asc'); $query = $this->db->get(); return $query->result(); // Produces SQL // SELECT * FROM tbl_Books ORDER BY Author asc;
Resultset:
![CodeIgniter Order By Asc codeigniter-order-by-asc-example](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7-i8geCGhEV-HWcV2s58dk4PCjCYUdFMADkI6cUB-CqPtwx_VunVutKvHIt400qhAgbEYI_8Rm1kOoqH3R_Ay68ilEuwSfQqkJaY8g0iDsoZ-bisojygHnB-h3cCbwY0Az7nkveU69Ywx/s1600/codeigniter-order-by-asc-example.png)
CodeIgniter Order By Desc:
To sort a field in descending order you have to use desc
keyword. The below codeigniter order by query example sorts the above resultset by author in reverse alphabetical order.
$this->db->select('*'); $this->db->from('tbl_Books'); $this->db->order_by('Author', 'desc'); $query = $this->db->get(); return $query->result(); // Produces SQL // SELECT * FROM tbl_Books ORDER BY Author desc;
Resultset:
![CodeIgniter Order By Desc codeigniter-order-by-desc-example](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIPdoUsm7oQRjAtf3p8wV26ZhwaoYz7-3ZrpjiS-FSxje4uI0S8Uwy0BMgup4OV5umLXhxLQp4FUqMPq6WACwZIEDWxslnThjCuMsirA9pgXGDW35uyXsSY8XL_lzI5fdXIlI6SPbaioaW/s1600/codeigniter-order-by-desc-example.png)
CodeIgniter Order By Two Columns:
You can also sort records using more than one column. This code igniter query sorts the table 'Books' with two columns, first in descending order of 'Category' and then ascending order of 'BookName'.
$this->db->select('*'); $this->db->from('tbl_Books'); $this->db->order_by('Category', 'desc'); $this->db->order_by('BookName', 'asc'); $query = $this->db->get(); return $query->result(); // Produces SQL // SELECT * FROM tbl_Books ORDER BY Category desc, BookName asc;
Resultset:
![CodeIgniter Order By Two Columns codeigniter-order-by-two-columns-fields](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJaxD262hbY0WfARgFdFSaUMsDG6D54yQ2t-SakEnMKeRwRqoD0C9xnvLU9exehVVKGvwb53mqpAjlieKrtwfxJY9dArzJl0PASck5JhWX7Vk_XJbigfs18G8i6CDN3JzfDFPAKxeodOWZ/s1600/codeigniter-order-by-two-columns-fields.png)
To set order by clause for two columns either you can use order_by() method twice (like above) or pass the parameters as a single string.
$this->db->order_by('Category desc, BookName asc');
Method Chaining in CodeIgniter:
If you think above query has so many lines, then simply shorten it using method chaining in codeigniter. But keep in mind this works only with PHP 5 or above.
$this->db->select('*')->from('tbl_Books')->order_by('Category desc, BookName asc'); $query = $this->db->get(); return $query->result(); // Produces SQL // SELECT * FROM tbl_Books ORDER BY Category desc, BookName asc;
CodeIgniter Order By Limit:
You can also control the number of rows returned by the query using LIMIT clause. Function limit() can be used along with order_by() like this,
$this->db->select('*'); $this->db->from('tbl_Books'); $this->db->order_by('Category', 'asc'); $this->db->limit(3, 1); $query = $this->db->get(); return $query->result(); // Produces SQL // SELECT * FROM tbl_Books ORDER BY Category ASC LIMIT 1, 3;
Resultset:
![CodeIgniter Order By Limit codeigniter-order-by-limit-example](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhM7Ia-fH-CE0He-lhlAoY5dP6b5pRibZwKu36v2PQHJTeGDmLNAhdxPj8pSk6Lj7bVUBq8835XswP5fd3_kZgo-QyoXoiEtBGgi75brenKSc12XjrbhjUt79yv_luvb0UDmORjrmV0ciox/s1600/codeigniter-order-by-limit-example.png)
You can shorten the above codeigniter sql query like this without using the limit()
function.
$this->db->order_by('Category', 'asc'); $query = $this->db->get('tbl_Books', 3, 1); return $query->result(); // Produces SQL // SELECT * FROM tbl_Books ORDER BY Category ASC LIMIT 1, 3;
That was all about working with codeigniter order by query. I hope you find all the above examples of order by query in codeigniter useful. Please let me know your queries through comments.
Good info, thanks
ReplyDelete