Posted inCodeigniter

CodeIgniter Order By Query Example

Today, We want to share with you order by codeigniter.In this post we will show you Codeigniter OrderBy Query not Working, hear for orderBy Query we will give you demo and example for implement.In this post, we will learn about Sort Query Order By In Codeigniter Examples with an example.

Codeigniter OrderBy Query

In CodeIgniter, you can use the order_by method to sort the results of a query. Here’s an example:

$this->db->order_by("column_name", "asc/desc");

In this example, the column_name is the name of the column you want to sort, and asc/desc is the sort direction, either ascending (asc) or descending (desc).

You can also chain multiple order_by methods to sort by multiple columns:

$this->db->order_by("column1_name", "asc/desc")->order_by("column2_name", "asc/desc");

In this example, the results will be sorted by column1_name first, and then by column2_name if there are any duplicate values in column1_name.

Note that the order_by method should be called before the get method to retrieve the results:

$query = $this->db->order_by("column_name", "asc/desc")->get("table_name");

Codeigniter Order By Query ASC Example

$this->db->from('my_shop');
$this->db->order_by("column1 asc,column2 asc");
$query = $this->db->get(); 
return $query->result();

Codeigniter Order By Query DESC Example

$this->db->from('my_shop');
$this->db->order_by("column1 desc,column2 desc");
$query = $this->db->get(); 
return $query->result();

Example

$this->db->select('*');
$this->db->from('tbl_Members');
$this->db->order_by('MemberName');
$query = $this->db->get();
return $query->result();

// Produces SQL
// SELECT * FROM tbl_Members ORDER BY MemberName;

CodeIgniter Order By Desc:

$this->db->select('*');
$this->db->from('tbl_Members');
$this->db->order_by('Cart', 'desc');
$query = $this->db->get();
return $query->result();

// Produces SQL
// SELECT * FROM tbl_Members ORDER BY Cart desc;

CodeIgniter Order By Two Columns:

You can also sort records using more than one column. This code igniter query sorts the table ‘Members’ with two columns, first in descending order of ‘Level’ and then ascending order of ‘MemberName’.

$this->db->select('*');
$this->db->from('tbl_Members');
$this->db->order_by('Level', 'desc');
$this->db->order_by('MemberName', 'asc');
$query = $this->db->get();
return $query->result();

// Produces SQL
// SELECT * FROM tbl_Members ORDER BY Level desc, MemberName asc;

Method Chaining in CodeIgniter:

$this->db->select('*')->from('tbl_Members')->order_by('Level desc, MemberName asc');
$query = $this->db->get();
return $query->result();

// Produces SQL
// SELECT * FROM tbl_Members ORDER BY Level desc, MemberName asc;

CodeIgniter Order By Limit:

$this->db->order_by('Level', 'asc');
$query = $this->db->get('tbl_Members', 3, 1);
return $query->result();

// Produces SQL
// SELECT * FROM tbl_Members ORDER BY Level ASC LIMIT 1, 3;

In CodeIgniter, the order_by() method is used to specify the ordering of the results returned by a database query. This method is typically used in conjunction with the get() or get_where() methods to retrieve data from a database table.

The order_by() method accepts two parameters: the name of the column to order by, and the direction of the ordering (either “asc” for ascending or “desc” for descending). Here is an example of how to use the order_by() method in CodeIgniter:

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

In this example, the select() method is used to specify that all columns should be returned from the my_table table. The from() method is used to specify the table to select data from, and the order_by() method is used to specify that the results should be ordered by the column_name column in ascending order.

After calling the get() method, the $query variable will contain the results of the query, which can then be processed and displayed as needed.

I hope you get an idea about order by codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype