I have 3 tables,category w/c consist of (category_id,name),ingredient w/c consist of (ingredient_id,name) and category_ingredient w/c consist of(ingredient_id,category_id). now i want to add a condition in the category_ingredient that can check if an ingredient and category is the same then i cant add that certain ingredient. can u guys help me?please
table
this is my code:
VIEW: add_ingredients.php
<?php echo form_open('dashboard/uploadIngredients', 'class="form-horizontal" enctype="multipart/form-data"'); ?>
<div class="form-group">
<div class="col-sm-10">
<select class="form-control" name="ingredient_category">
<option 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)"></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(); ?>
CONTROLLER: dashboard.php
private function checkExist($param1,$param2){
$res=$this->recipick->findExist($param1,$param2);
if($res>0){
return true;
}else{
return false;
}
}
public function uploadIngredients()
{
$this -> form_validation -> set_rules("name", "Name", "required|xss_clean|is_unique[recipick.ingredient_id]");
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
$saveData[] = array('ingredient_id' => null,
'name' => trim($value)
);
}
$ingredient_id = $this->products_model->saveIngredients($saveData);
foreach (explode(',', $this->input->post('ingredient_category')) as $key => $value)
{
foreach ( $ingredient_id as $key => $str ){
$joinData[] = array(
'ingredient_id' => $str,
'category_id' => intval($value)
);
}
$this->products_model->saveCategoryIngredients($joinData);
redirect('dashboard/add_ingredients');
}
}
MODEL:products_model.php
public function saveIngredients($data) {
foreach($data as $row => $value)
{
$this->db->insert('ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
return $insert_id;
}
public function saveCategoryIngredients($data)
{
foreach($data as $row => $value)
{
$this->db->insert('category_ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
return $insert_id;
}
public function findExist($param1,$param2)
{
$query=$this->db->query("select * category_ingredient where ingredient_id='".$param1."' and category_id='".$param2."'");return count($query);}
but i can still add same same ingredient with same category
Aucun commentaire:
Enregistrer un commentaire