Pass Array to Where in Codeigniter Active Record

Passing array to like active record in codeigniter

You can't just pass an array to the like() function, it has to be a string. You need to apply a like clause for each of the keywords in the string.

You need to use or_like to match a record to either of the keywords, however, because if you just use like() each time, it will need to match all of the keywords due to the query being like LIKE "%smart" AND LIKE "%intelligent" etc. and that isn't what you require.

$array_string = "smart,intelligent,awesome";
$array_like = explode(',', $array_string);
foreach($array_like as $key => $value) {
if($key == 0) {
$this->db->like('topic', $value);
} else {
$this->db->or_like('topic', $value);
}
}

Try this way to avoid your problem of the rest of the where statement being ignored.

$array_string = "smart,intelligent,awesome";
$array_like = explode(',', $array_string);

$like_statements = array();

foreach($array_like as $value) {
$like_statements[] = "topic LIKE '%" . $value . "%'";
}

$like_string = "(" . implode(' OR ', $like_statements) . ")";

The value of $like_string will be (topic LIKE '%smart%' OR topic LIKE '%intelligent%' OR topic LIKE '%awesome%')

You can then use $like_string in the following way with ActiveRecord:

$this->db->where($like_string, FALSE);

Pass array to where in Codeigniter Active Record

$this->db->where_in('id', ['20','15','22','42','86']);

Reference: where_in

ActiveRecord where_in() with array

The array you try to pass is a multi dimensional array. Instead try this:


$ids = array();
foreach ($query->result_array() as $id)
{
$ids[] = $id['id'];
}

$this->db->where_in('id', $ids);

You can not flatten the query->result_array() without iteration. But if you need to handle this kind of queries a lot in your application, and if you have >= PHP 5.3 installed, you could put the following function in a Codeigniter helper file (or somewhere else suitable) to help you flattening arrays:


function flatten(array $array) {
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}

And in your case use it like this:


$ids = flatten($query->result_array());
$this->db->where_in('id', $ids);

Codeigniter pass array from Model to Controller

You have to use Codeigniter's Active Record Query.
In Model (recommended) :

$this->db->select('car_id');
$this->db->from('cars');
$this->db->where('price',1);
$query=$this->db->get();
return $query->result();

Updated Without Active record (Not recommended) :

$sql = "
SELECT car_id
FROM cars
WHERE price= 1";

$res = mysql_query($sql);

$bought_cars= array();
while ($row_ya = mysql_fetch_object($res))
{
$bought_cars[] = $row_ya->car_id;
}
return $bought_cars;//no need to run foreach, you just have to return the array after completing your push operation.

Codeigniter active record where array

http://ellislab.com/codeigniter/user-guide/database/active_record.html

$whereQuery['service.service_end_date >'] = $start;
$whereQuery['service.service_end_date <'] = $start;

You can pass > < <> in CI where function

$this->db->where('field_name <', "Condition_value"); 

Codeigniter Active Record, Where in

->where() you can use 2nd and 3rd argument, so that any string you can pass

$this->db->where('`kd_x`  IN (select `kd_x` from `MT_master` where a="11921212")', NULL, FALSE);

OR

//Create where clause
$this->db->select('kd_x')
->where('a','11921212')
->from('MT_master');
$where_clause = $this->db->get_compiled_select();

//Create main query
$this->db->select('*');
->from('your_table');
->where("`kd_x` IN ($where_clause)", NULL, FALSE);


Related Topics



Leave a reply



Submit