samedi 23 janvier 2016

check if ingredient exist

I have a table ingredient w/c consist of(ingredient_id,name). I can perfectly add an ingredient but when i tried to add a condition that prohibit an ingredient that exist in my table to be added nothing works. please help me

HERE'S MY CODE:

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(); ?>

CONTROLLER:

 public function uploadIngredients()
{
    foreach(explode(',', $this->input->post('ingredients')) as $key => $value) {
        $saveData[] = array('ingredient_id' => null,
                            'name'  => trim($value)
        );  
    }

    // var_dump($saveData); die();
    $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;
}

Aucun commentaire:

Enregistrer un commentaire