【问题标题】:prohibit adding existing value in a multidimensional array禁止在多维数组中添加现有值
【发布时间】:2016-01-24 06:38:36
【问题描述】:

我有一个表成分 w/c 由(ingredient_id,name)组成。我可以完美地添加一种成分。但我想添加一个禁止添加现有成分的条件。我有该条件的代码,但我认为这是错误的,因为条件不起作用,我仍然可以添加现有成分。请帮帮我

这是我的代码:

查看:

 <?php echo form_open('dashboard/uploadIngredients', 'class="form-horizontal" enctype="multipart/form-data"'); ?>
        <div class="form-group">
            <div class="col-sm-10">

                <select required class="form-control" name="ingredient_category">

                    <option value="" selected disabled>Select Ingredient Category</option>
                <option value="All">All</option>
                <?php foreach($this->products_model->getCategory() as $row): ?>
                    <option value="<?php echo $row->category_id ?>"><?php echo $row->name; ?></option>
                <?php endforeach; ?>
                </select>

            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-10">
                <textarea class="form-control" name="ingredients" rows="5" placeholder="Ingredients (EX. onion, oil, pasta)" required></textarea> 
            </div>
        </div>

        <div class='form-group'>
            <div class="col-sm-10">
                <button class="btn btn-lg btn-positive" type="submit"><i class="glyphicon glyphicon-ok"></i> Save Ingredient</button>
            </div>
        </div>
    <?php echo form_close(); ?>

控制器:

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

【问题讨论】:

  • 试着集中你的问题并指出与_添加一个禁止我的表中存在的成分的条件_有关的部分。然后人们会更容易帮助你。
  • 你说什么都没用是什么意思?输出是什么?
  • 对不起先生@Michael。先生,我接受您的建议。我现在编辑了我的问题:)
  • 输出是我仍然可以添加现有的成分先生。就像我的条件不起作用@user3653438
  • 您在控制器中将成分 ID 设置为 null,因此您将永远找不到匹配项。您不想在成分 ID 上进行比较,而是在名称上进行比较。

标签: php arrays codeigniter if-statement multidimensional-array


【解决方案1】:

将您的模型更改为

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

应该这样做。只需更改 $query 行。

如果这不起作用,请对该行上方的 $value 执行 var_dump,以便查看是否需要调整目标值。

【讨论】:

  • 还是不行,先生。我将在哪里进行 var_dump?在模型中?
【解决方案2】:

使用下面的代码。

控制器:

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

型号:

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

【讨论】:

  • 还是不行,先生,因为我认为我的这个问题是关于多维数组的:(
猜你喜欢
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
相关资源
最近更新 更多