2016-01-24 48 views
0

我有一個表格成分w/c包含(ingredient_id,name)。我可以完美地添加一種成分。但我想添加一個條件,禁止添加現有的成分。我有這種情況的代碼,但我認爲這是錯誤的,因爲條件不起作用,我仍然可以添加現有的成分。請幫我禁止在多維數組中添加現有值

這裏是我的代碼:

VIEW:

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

MODEL:

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

嘗試專注你的問題,並指出,有做部分_添加禁止存在於我的table_成分的條件。那麼人們會更容易幫助你。 – Michael

+0

當你說什麼都行不通時,你是什麼意思?什麼是輸出? – adarshdec23

+0

對不起@Michael先生。我接受你的建議先生。我現在編輯我的問題:) – Kimbhe

回答

0

模型更改爲

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

這應該這樣做。只需更改$查詢行。

如果這不起作用,請在該行上方的$ value上執行var_dump,以便您可以查看是否需要調整您的目標值。

+0

仍然不起作用先生。我將在哪裏var_dump?在模型中? – Kimbhe

0

使用下面的代碼。

控制器:

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

MODEL:

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

仍然不工作先生,因爲我認爲我的這個問題是關於多維陣列:( – Kimbhe