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.

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.

$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 :

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.   How to get last record from MySQL table in Laravel?
  2.   Get the Last Inserted Id Using Laravel Eloquent
  3.   Creating Dynamic Email Template in PHP
  4.   laravel 5/6/7 open link in new tab Example
  5.   Nodejs Login and Registration with PHP MySQL and SQLite Example
  6.   PHP How-to create Dynamic PDF Files using FPDF
  7.   Create Dynamic XML Sitemaps Using PHP MYSQL
  8.   Laravel 5.8 - Get Last Inserted ID With Example
  9.   Simple Laravel Log Viewer API Example
  10.   Set Cookies Get Cookies Delete Cookies with PHP
Tags:, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,