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
Convert Ascii to Utf-8 Encoding
Sqlsrv_Num_Rows Not Returning Any Value
PHP Exec $Path Variable Missing Elements
Custom User Authentication Base on the Response of an API Call
What's the Best Way to Pass a PHP Variable to JavaScript
Will Copy-On-Write Prevent Data Duplication on Arrays
PHP Startup Unable to Load Dynamic Library PHP_Mongo.Dll
Get Youtube Video Id from HTML Code with PHP
The Advantage/Disadvantage Between Global Variables and Function Parameters in PHP
What Does This Mean in Documentation: Square Bracket Followed by Comma ( [, )
Populate Another Select Dropdown from Database Based on Dropdown Selection
Unexpected Character in Input: '\' (Ascii=92) State=1
Casting an Array with Numeric Keys as an Object
PHP Recursion to Get All Possibilities of Strings
How to Add a Delete Button to a PHP Form That Will Delete a Row from a MySQL Table