gpt4 book ai didi

php - 将数组从 Controller 传递到模型 CodeIgniter

转载 作者:行者123 更新时间:2023-11-29 20:58:32 24 4
gpt4 key购买 nike

我在 Code Igniter 中将数组从 Controller 传递到模型以进行进一步处理时遇到了麻烦。

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);
}

型号:

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();

}

错误:

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

当将相同的数组放置在 Model 中的 my_function() 内部时,效果很好。

例如:

public my_function(){

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

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


}

这里可能出现什么问题?任何帮助将不胜感激。

提前致谢。

最佳答案

请像这样传递数组

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);
}

关于php - 将数组从 Controller 传递到模型 CodeIgniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37426352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com