【发布时间】:2021-07-31 11:45:15
【问题描述】:
我的视图中有一个名为categories[] 的多项选择,其中包含我表上id 的值。我想从categories[] 中获取值并用它创建一个where 子句:Where id = categories[] selected values。但是,它是一个数组。如何使用数组值设置 Where 子句?所以我可以从categories[]中获取id具有选定值的数据
我的表中有 3 列:
(id, category, name).
我的表的值是:
(1, "food", "Pizza")
(2, "food", "Burger")
(3, "drink", "Mineral Water")
(4, "drink", "Tea")
(5, "snack", "Apple Pie")
我所做的是:通过在我的视图中执行此命令来获取选定的值:
var v = $('[name="categories[]"]').val();
//and doing like this, so I can get it to my controller
"data": 'cat=' + v,
然后,我在控制器中得到选择的值,如下所示:
public function getData(){
$cat = $this->input->post('cat');
$where = array('id' => [$cat]);
$result = $this->my_model->getData($where);
//and some code to set the data in my table
//and code for return the data
}
最后,我在我的模型中设置了 WHERE 子句,如下所示:
public function getData($where){
$this->db->select('*');
$this->db->from($this->table);
$this->db->where($where);
$query = $this->db->get();
return $query->resut();
}
我在上面所做的查询如下:
Unknown column 'Array' in 'where clause'
SELECT `id`, `category`, `name` FROM `my_table` WHERE `id`= Array ORDER BY `category` ASC
【问题讨论】:
标签: php arrays ajax codeigniter where-clause