I have the problems using the array input as a value in WHERE clause.
But don't want to use more than once in WHERE clause code.
In my case, this is what I want :
$cond = array('job_id' => $job_id_var, 'job_name' => $job_name_var);
//WHERE clause
$this->where($cond); //only using once WHERE clause code like this, array as input
//which means
WHERE job_id = '$job_id_var' AND job_name = '$job_name_var'
is it possible to do that in codeigniter?
Yes, the ->where() method can support that.
Since you do not want to cascade it:
$this->db->where('job_id', $job_id_var);
$this->db->where('job_name', $job_name_var);
->where() can handle array input as well:
$cond = array('job_id'=>$job_id_var, 'job_name'=>$job_name_var);
$this->db->where($cond); // here, only used once.
$query = $this->db->get('hello_table');
$result = $query->result_array();
return $result;
Related
I'm trying use a whereIn inside a where array I am passing to Laravel query Builder:
$where = [['Participants.Client_Id','IN', $clientId]];
DB::table('Participants')->where($where)->get()
Something like is what I want to achieve, and I know there are works around such as using whereIn, but I'm sharing here a small piece of code to give you an idea, so I need to change the array to make it works as a whereIn, not changing the ->where to ->whereIn or ->whereRaw
DB::table('participants)->whereIn('Participants.Client_Id',$clientId)->get();
You must collect the IDs in the $clientId variables.
If I understand, you could do something like that :
$wheres = [['Participants.Client_Id','IN', [$clientId]]];
$query = DB::table('Participants');
foreach($wheres as $where) {
$query->where($where[0], $where[1], $where[2]);
}
$participants = $query->get();
As laravel document , you can use array in where and each element of this array must be a array with three value . So your $where variable is correct.
But as I searched in operator is not supported by query builder of where.
I have a table, myTable, with a single column, myColumn, with a single row.
I want to change the value of myColumn in that first (and only) row.
I tried this, but nothing happens:
$myNewValue = 'foo';
$this->db->update('myColumn', $myNewValue);
What am I doing wrong?
Obviously it must have something to do with not specifying which table, but I don't know how to do that.
Codeigniter's update() needs to follow this syntax:
update([$table = ''[, $set = NULL[, $where = NULL[, $limit = NULL]]]])
Parameters:
$table (string) – Table name
$set (array) – An associative array of field/value pairs
$where (string) – The WHERE clause
$limit (int) – The LIMIT clause
so besides of adding the correct table name, you'd need to send update data as an array, using this approach:
$myNewValue = array('myColumn'=>'foo');
$this->db->update('myTable', $myNewValue);
Figured out how to do it:
$this->db->update('myTable', [
'myColumn' => $myNewValue,
]);
Try this method, it is my favorite format.
$where = array(
'column_id'=$id
);
$data = array(
'column_name'=$data
);
$this->db->update('table_name', $data,$where);
i want to get data using where in condition
my code is below
SELECT `cuc_UserCardsDetail`.*, `cuc_CreditCardType`.`varValidName`
FROM (`cuc_UserCardsDetail`)
JOIN `cuc_CreditCardType` ON `cuc_CreditCardType`.`intGlCode` =
`cuc_UserCardsDetail`.`fk_CardTypeGlCode`
WHERE `cuc_UserCardsDetail`.`intGlCode` IN ('10,29')
AND `chrAccountType` = 'M'
ORDER BY `dtCreateDate` desc
now I have resulted with one row actually in my table there is two-row with related id
i think problem is In('10,29') but i want like ('10','29') or (10,29) how can i do in codeigitor?
i want (10,29) or ('10','29') in where in ..
In CI query builder use
$id= array('10', '20'); # or direct assing your input params
$this->db->where_in('column_name', $id);
Read Looking for Specific Data - Codeigniter.com
Method chaining will accept array in where_in
$intGlCode = array(10, 29);
$this->db->select('`cuc_UserCardsDetail`.*, `cuc_CreditCardType`.`varValidName`');
$this->db->from('cuc_UserCardsDetail');
$this->db->join('cuc_CreditCardType','cuc_CreditCardType.intGlCode =
cuc_UserCardsDetail.fk_CardTypeGlCode');
$this->db->where('chrAccountType','M');
$this->db->where_in('cuc_UserCardsDetail.intGlCode',$intGlCode);
$this->db->order_by('dtCreateDate','DESC');
$data = $this->db->get()->result_array();
Your query look something like
you have to define column chrAccountType from which table, here i assume it from cuc_UserCardsDetail
$this->db->select('ud.*, cc.varValidName');
$this->db->where_in('ud.intGlCode', ['10','29']);
$this->db->where('ud.chrAccountType', 'M');
$this->db->order_by('dtCreateDate', 'desc');
$this->db->join('cuc_CreditCardType cc', 'cc.intGlCode = ud.fk_CardTypeGlCode');
$data = $this->db->get('cuc_UserCardsDetail ud')->result_array();
print_r($data);
where_in() function just in the case of array of data.
$ids = array();
$this->db->where_in('id', $ids);
I was wondering how can I build a condition based query in Laravel using eloquent?
I've found how to do it with a raw query but that that's not what I want also the answer to this question isn't that dynamic at least not as dynamic as I want it to be.
What I try to achieve is to create a dynamic WHERE query based on certain conditions, for example if the field is filled or not.
If I use the following code,
$matchThese = [
'role' => 'user',
'place' => \Input::get('location')
];
$availableUsers = User::where($matchThese)->take($count)->orderByRaw("RAND()")->get();
The query will fail if I don't send a location as POST value. I don't want it to fail I want it to skip to the next WHERE clause in the query. So basically if there's no place given don't search for it.
Build up the query and include the ->where() clause depending on whether or not you have the location in your input:
$query = User::where('role', 'user');
$query = \Input::has('location') ? $query->where('location', \Input::get('location')) : $query;
$availableUsers = $query->take($count)->orderByRaw('RAND()')->get();
Just build the array with an if condition:
$matchThese = [
'role' => 'user',
];
if(\Input::has('location')){
$matchThese['place'] = \Input::get('location');
}
$availableUsers = User::where($matchThese)->take($count)->orderByRaw("RAND()")->get();
$query = DB::table('table_name');
if($something == "something"){
$query->where('something', 'something');
}
$some_variable= $query->where('published', 1)->get();
You can use something like this.
Say you construct an activerecord query that will always just return a single value, how do you just address that single value instead of getting an array in return? For instance I am using an ActiveRecord query to return the SUM of a single column, it will only return this one single SUM, instead of having to parse the array is there a way to assign the value as a function return equal to that value instead of getting an array?
$query = $this->db->get(); // your ActiveRecord query that will return a col called 'sum'
echo $query->row('sum');
$output = $this->db->query("YOUR QUERY")->row()->fieldname;
OR
$output = $this->db->get("TABLE NAME")->row()->fieldname;
Where fieldname is the name of the field you want the output of in your query.
this isn't the prettiest way. i can't find a way to do it with the active record api. but here's a one-liner to get it.
$result = array(
array('sum' => '23')
);
echo current( current( $result ) );