Php 禁止在多维数组中添加现有值
我有一个包含(配料id、名称)的表配料。我完全可以添加一种配料。但我想增加一个条件,禁止添加现有成分。我有这个条件的代码,但我认为它是错误的,因为条件不起作用,我仍然可以添加现有的成分。请帮帮我 这是我的密码: 视图: 型号:Php 禁止在多维数组中添加现有值,php,arrays,codeigniter,if-statement,multidimensional-array,Php,Arrays,Codeigniter,If Statement,Multidimensional Array,我有一个包含(配料id、名称)的表配料。我完全可以添加一种配料。但我想增加一个条件,禁止添加现有成分。我有这个条件的代码,但我认为它是错误的,因为条件不起作用,我仍然可以添加现有的成分。请帮帮我 这是我的密码: 视图: 型号: public function saveIngredients($ingredient_id) { foreach($ingredient_id as $row => $value) { $query=$this->db->whe
public function saveIngredients($ingredient_id)
{
foreach($ingredient_id as $row => $value) {
$query=$this->db->where('ingredient_id', $value->ingredient_id);
if($query->num_rows > 0) {
echo '<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>';
echo "Ingredient already taken";
echo '</strong></div>';
} else {
$this->db->insert('ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
}
return $insert_id;
}
public function saveIngredients($data)
{
$query=$this->db->where('name', $data);
if($query->num_rows > 0) {
return ['status'=>FALSE];
} else {
$this->db->insert('ingredient', array('name'=>$data));
$insert_id = $this->db->insert_id();
return ['status'=>TRUE,'id'=>$insert_id];
}
}
public函数保存成分($component\u id)
{
foreach($row=>$value){
$query=$this->db->where('component\u id',$value->component\u id);
如果($query->num\u rows>0){
回声“×”;
echo“已服用的成分”;
回音“”;
}否则{
$this->db->insert('component',$value);
$insert_id[]=$this->db->insert_id();
}
}
返回$insert_id;
}
将您的型号更改为
public function saveIngredients($ingredient_id)
{
foreach($ingredient_id as $row => $value) {
$query=$this->db->where('name', $value->name);
if($query->num_rows > 0) {
echo '<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>';
echo "Ingredient already taken";
echo '</strong></div>';
} else {
$this->db->insert('ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
}
return $insert_id;
}
public函数保存成分($component\u id)
{
foreach($row=>$value){
$query=$this->db->where('name',$value->name);
如果($query->num\u rows>0){
回声“×”;
echo“已服用的成分”;
回音“”;
}否则{
$this->db->insert('component',$value);
$insert_id[]=$this->db->insert_id();
}
}
返回$insert_id;
}
应该这样做。只是更改$query行
如果这不起作用,请在该行正上方的$value上进行var_转储,以便查看是否需要调整目标值。使用下面的代码
控制器:
public function uploadIngredients()
{
foreach(explode(',', $this->input->post('ingredients')) as $key => $value) {
$saveData[] = array('ingredient_id' => null,
'name' => trim($value)
);
}
$ingredient_id = $this->products_model->saveIngredients($saveData);
redirect('dashboard/add_ingredients');
} }
public function uploadIngredients()
{
foreach(explode(',', $this->input->post('ingredients')) as $key => $value) {
$result = this->products_model->saveIngredients(trim($value));
if($result['status'])
{
$ids[]=$result['id'];
}else{
echo '<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>';
echo "Ingredient already taken";
echo '</strong></div>';
}
}
redirect('dashboard/add_ingredients');
} }
试着聚焦你的问题并指出与之相关的部分uu添加禁止我的表格中存在的成分的条件uu。这样人们就更容易帮助你了。你说什么都不管用是什么意思?输出是什么?对不起,迈克尔先生。我接受你的建议,先生。我现在编辑了我的问题:)结果是我仍然可以添加现有的成分,先生。就像我的条件在@user3653438不起作用一样,您正在控制器中将您的id设置为null,所以您永远找不到匹配项。您不想在配料id上进行比较,而是在名称上进行比较。仍然不起作用,先生。我将在模型中的变量转储到哪里?仍然不起作用,先生,因为我认为我的这个问题是关于多维数组的:(
public function saveIngredients($data)
{
$query=$this->db->where('name', $data);
if($query->num_rows > 0) {
return ['status'=>FALSE];
} else {
$this->db->insert('ingredient', array('name'=>$data));
$insert_id = $this->db->insert_id();
return ['status'=>TRUE,'id'=>$insert_id];
}
}