sort query order by in codeigniter Examples

Today, We want to share with you sort query order by in codeigniter .In this post we will show you order by in ci, hear for multiple column orderby in codeigniter we will give you demo and example for implement.In this post, we will learn about orderby in codeigniter with an example.

sort query order by in codeigniter

There are the Following The simple About order by desc in codeigniter Full Information With Example and source code.

As I will cover this Post with live Working example to develop order_by in codeigniter, so the sort by alphabetical order in codeigniter is used for this example is following below.

sort query orderby in codeigniter
sort query orderby in codeigniter

order_by() Query in Codeigniter

The CI Based order from() function is used in the simple MySQL Database table ascending as well s descending order to sorting or arrange the all the Data result-set. The first parameter added the Database table column Like as a product_title by which you want to any order Like as a ASC or DESC. The second parameter allows you to set the result’s course. It’s ASC, DESC as well as RANDOM selection data.

Read Also:  PHP SEND mail with Multiple Attachments

MySQL Database Table Sort table data in ascending or descending order in Codeigniter

using DESC

orderby descending in codeigniter

$this->db->order_by('product_code', 'DESC');
// Stored Produces: ORDERBY `product_code` DESC

Using Passing Data with ASC

orderby asc in codeignitersimple We can also pass the arguments your own data string MySQL table in the 1st parameter

$this->db->order_by('product_code DESC, product_title ASC');
// Stored Produces: ORDERBY `product_code` DESC, `product_title` ASC

Using multiple function & fields

Or multiple method calls can be made if you required the one or more multiple fields.

Read Also:  html web page examples with source code

$this->db->order_by('product_code', 'DESC');
$this->db->order_by('product_title', 'ASC');
// Stored Produces: ORDERBY `product_code` DESC, `product_title` ASC

Usiong RANDOM

If you select the RANDOM direction option.orderby random in codeigniter

$this->db->order_by('product_code', 'RANDOM');
// Stored Produces: ORDERBY RAND()

$this->db->order_by(50, 'RANDOM');
// Stored Produces: ORDERBY RAND(50)

CodeIgniter Order By Query Example

MySQL Table: Doctors

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

// Produces SQL
// SELECT * FROM table_Doctors ORDERBY DoctorName;

CodeIgniter Order By Asc:

$this->db->select('*');
$this->db->from('table_Doctors');
$this->db->order_by('hospital', 'asc');
$query = $this->db->get();
return $query->result();

// Produces SQL
// SELECT * FROM table_Doctors ORDERBY hospital asc;

CodeIgniter Order By Desc:

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

// Produces SQL
// SELECT * FROM table_Doctors ORDERBY hospital desc;

CodeIgniter Order By Two Columns:

$this->db->select('*');
$this->db->from('table_Doctors');
$this->db->order_by('Staff', 'desc');
$this->db->order_by('DoctorName', 'asc');
$query = $this->db->get();
return $query->result();

// Produces SQL
// SELECT * FROM table_Doctors ORDERBY Staff desc, DoctorName asc;

Method Chaining in CodeIgniter:

$this->db->select('*')->from('table_Doctors')->order_by('Staff desc, DoctorName asc');
$query = $this->db->get();
return $query->result();

// Produces SQL
// SELECT * FROM table_Doctors ORDERBY Staff desc, DoctorName asc;

CodeIgniter Order By Limit:

$this->db->select('*');
$this->db->from('table_Doctors');
$this->db->order_by('Staff', 'asc');
$this->db->limit(3, 1);
$query = $this->db->get();
return $query->result();

// Produces SQL
// SELECT * FROM table_Doctors ORDERBY Staff ASC LIMIT 1, 3;

Web Programming Tutorials Example with Demo

Read :

Read Also:  Laravel 7 Custom Pagination View Example Tutorial

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about limit in 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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Woocommerce Get product image url from an image_id
  2. Read Also:  PHP Change Password script using Mysqli
  3. Read Also:  Query Scope using Laravel 6 Eloquent Example
  4. Read Also:  Simple CodeIgniter 3 login MySQL Database
  5. Read Also:  Select DropDown Option Selected In Laravel
  6. Read Also:  PHP Loop: For, ForEach, While, Do While [Example - for loop in php]
  7. Read Also:  How to Schedule Cron Jobs in laravel?
  8. Read Also:  Codeigniter or where Condition or in Clauses
  9. Read Also:  PHP Loop: For, ForEach, While, Do While [Example - for loop in php]
  10. Read Also:  PHP SEND mail with Multiple Attachments
CLOSEX