Tags
Asked 2 years ago
17 Jun 2021
Views 162
Pietro

Pietro posted

How can use order-by in codeigniter?

How can use order-by in codeigniter?
jignesh

jignesh
answered Apr 27 '23 00:00

You can use the $this->db->order_by() method in CodeIgniter to sort the results of your query in ascending or descending order.

Here's an example of how to use $this->db->order_by() to sort a query result by a column in ascending order:



$this->db->select('*');
$this->db->from('my_table');
$this->db->order_by('my_column', 'ASC');
$query = $this->db->get();

In this example, we are selecting all columns from a table named "my_table" and ordering the results by the "my_column" column in ascending order.

To sort the results in descending order, you can replace "ASC" with "DESC" like this:



$this->db->select('*');
$this->db->from('my_table');
$this->db->order_by('my_column', 'DESC');
$query = $this->db->get();

In this example, we are selecting all columns from a table named " my_table " and ordering the results by the " my_column " column in descending order.

You can modify the example to suit your specific query needs, changing the table name, column names, and values as necessary.

Note that you can also use the $this->db->order_by() method multiple times in a query to sort the results by multiple columns.
Post Answer