Fetch data with using order, ASC, DESC, RANDOM

Deepak Tailor Image
Deepak Tailor - Oct 30 2020
Fetch data with using order, ASC, DESC, RANDOM

We do the ordering method for sorting the data then. In the first parameter we pass the column name and set the type in the second parameter. Codeigniter provided three type of order like as : ASC order for old records first, DESC order for new records first, RANDOM order show randomly records.

ASC order

The oldest record in ASC order shows at first position.

public function order_data()
{
	$query = $this->seller->table('users')
	->orderBy('id','ASC')
	->get();
	echo "<pre>";
	print_r($query->getResult());
}
DESC order

The latest record in DESC order is the show at first position.

public function order_data()
{
	$query = $this->seller->table('users')
	->orderBy('id','DESC')
	->get();
	echo "<pre>";
	print_r($query->getResult());
}
RANDOM order

The record in RANDOM order shows in random positions.

public function order_data()
{
	$query = $this->seller->table('users')
	->orderBy('id','RANDOM')
	->get();
	echo "<pre>";
	print_r($query->getResult());
}
Fetch data with limit

If the records have to be hold to some limit, then we do the limit.

public function order_data()
{
	$query = $this->seller->table('users')
	->limit(3)
	->get();
	echo "<pre>";
	print_r($query->getResult());
}
Set offset in records

In limit we can set offset in second parameter.

public function order_data()
{
	$query = $this->seller->table('users')
	->limit(4,1)
	->get();
	echo "<pre>";
	print_r($query->getResult());
}
Count records with countAllResult method

The countAllResult method is used to aggregate the database query records.

public function order_data()
{
	$query = $this->seller->table('users')
	->countAllResults();
	echo $query;
}

public function order_data()
{
	$query = $this->seller->table('users')
	->countAll();
	echo $query;
}
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry for the last 5 years. For professional and customize web development, you can send inquiry on our email.
----
You can contact him at support@fulstackdev.com