How to Add an Order by Clause Using Codeigniter's Active Record Methods

How to add an ORDER BY clause using CodeIgniter's Active Record methods?

I believe the get() function immediately runs the select query and does not accept ORDER BY conditions as parameters. I think you'll need to separately declare the conditions, then run the query. Give this a try:

$this->db->from($this->table_name);
$this->db->order_by("name", "asc");
$query = $this->db->get();
return $query->result();

CodeIgniter Documentation order_by()

Conditional clauses using Codeigniter's Active Record

This type of queries is also called "dynamic queries" because you can combine the clauses in an easy way. Try this:

$query = $this->db->select()
->from('db');
if(1==$case) {
$query->where('x',$something)
->where('y','fixed')
}
elseif (2==$case){
$query->where('x','fixed')
->where('y',$something)
}
....
return $query->get();

Answering your second question, you can use:

$this->db->like('category', '%');

Add a clause to a MySQL statement without quotes using CodeIgniter's Active Record functions

This will work.

$this->db->set('received_qty', 'received_qty + 1', FALSE);
$this->db->set('received_date', date("Y-m-d"));
$this->db->where($where);
$this->db->update('vrs_distribution');

ActiveRecord escapes everything put into a set(), where() and many other methods. Where and set can both take an optional third parameter of $escape which is a boolean. Set it to FALSE and CodeIgniter will not escape anything, meaning your field increment wont be treated as a string.

Sort array of objects in ascending order inside a function in PHP

You could use this.

Instead of sorting your data in php, you can do it in your query. In order to do that, you could use this code.

assuming that your function is get_todays_deals().

1st add parameter to the function. Something like this:

public function get_todays_deals($sortByField = '',$sortType = ''){
if($sortByField == 'expirationTime' && $sortType == 'ASC' ) {
# then do code query for sorting
# SELECT * FROM table_name ORDER BY $sortByField $sortType;
}
}

making the parameter equaling to '' so that you can still use your function without parameter.

Codeigniter Active Record issue (where and multiple 'AND' 'OR')

I believe you can do this in a few ways. First, I am assuming you are passing in an array to the model function and that array is $filters.

Option One

$this->db->select('*');
$this->db->from('tbl_admins');
$this->db->where('email', $filters["email"]);
$this->db->or_where('uname', $filters["email"]);
$this->db->where('upass', $filters["password"]);

Option Two

$this->db->select('*');
$this->db->from('tbl_admins');
$this->db->where("(email = '$filters["email"]' OR uname = '$filters["email"]') AND upass = '$filters["password"]'");

I did not test, so you may need to adjust the variables in option two's where a bit. But that is fairly close to what you want.

Here is another example of a similar Stack Overflow question.



Related Topics



Leave a reply



Submit