I tryng to pass an array of values from my controller to the model
My controller:
$arr_list = array('O','N','H');
$data['lista'] = $getmodel->query_list($arr_list1);
My model
public function query_list($list = '', $limit = 10, $lang = 'en')
{
$query = "SELECT tab1.id, name FROM tab_name";
$a = 0;
foreach( $list as $value)
{
if ($value != ''){
if ($a == 0){
$query .= " AND (field = '".$value."' ";
}
else
{
$query .= " OR field = '".$value."' ";
}
$a = $a + 1;
}
}
$query .= " ORDER BY RAND() ";
}
I receive the error: Array to string conversion
This code works perfectly in CI 2 but does not work on CI4.
Why?
Lots of bugs in yours the code, but I'll try to help.
Generates a WHERE field IN (‘item’, ‘item’) SQL query joined with AND if appropriate
$names = ['Frank', 'Todd', 'James'];
$builder->whereIn('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')
Controller
$this->data['return'] = $HotelModel->test(['1', '2', '3', '4']);
Model
public function test($array)
{
return $this->db->table('tab_name')->whereIn('column',$array)->get()->getResultObject();
}
Result last_query
SELECT * FROM 'tab_name' WHERE 'column' IN ('1', '2', '3', '4')