0

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?

1
  • 1. Where do you get the error? 2.Your function - query_list() does not return anything. 3. Where have you defined $arr_list1? It's nowhere to be seen. 4. Are you sure this worked "perfectly" in CI 2???? Commented Nov 24, 2020 at 12:31

1 Answer 1

2

Lots of bugs in yours the code, but I'll try to help.

Docs CI4: $builder->whereIn()

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.