简体   繁体   中英

Passing Array from Controller to Model CodeIgniter

I ran into trouble while passing an array from Controller to Model in Code Igniter for further processing.

Controller:

public function list_out(){

    $this->show_pagination();

    $action = array(
        'page'=>'public/'.$this->class_name.'/list',
        'title'=>'My Title',
        'success'=>$this->success,
        'data'=>$this->public_model->my_function(array('make'=>'Apple','color'=>'Black'))
    );

    $this->public_page($action);
}

Model:

public function my_function($arr){      

    $this->db->select('main_tbl.*,image_table.image_url');
    $this->db->from('main_tbl');        
    $this->db->join('image_tbl','image_tbl.prod_id=main_tbl.prod_id');
    $this->db->where('main_tbl.prod_status',1);
    $table = 'main_tbl';

    for ($i = 0; $i <  count($arr); $i++) {
        $key=key($arr);
        $val=$arr[$key];
        if ($val<> ' ') {
            $this->db->where($table.'.'.$key,$val);
        }
        next($arr);
    }   

    $this->db->where('image_tbl.image_cover',1);
    $this->db->order_by('prod_id','desc');
    $query = $this->db->get();  
    return $query->result();

}

Error :

Message: key() expects parameter 1 to be array, string given Message: next() expects parameter 1 to be array, string given

When the same array is placed inside my_function() in Model, it works well.

Ex:

public my_function(){

  $arr = array(
      'make'=>'Apple',
      'color'=>'Black'
  );

  $this->db->select('main_tbl.*,image_table.image_url');
  ........


}

What could be the issue here? Any help would be highly appreciated.

Thanks in advance.

Please pass array like that

public function list_out(){

    $this->show_pagination();
    $arr['make'] = 'Apple';
    $arr['color'] = 'Black';
    $action = array(
        'page'=>'public/'.$this->class_name.'/list',
        'title'=>'My Title',
        'success'=>$this->success,
        'data'=>$this->public_model->my_function($arr)
    );

    $this->public_page($action);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM