简体   繁体   中英

CODEIGNITER: check if ingredient exist

I have table ingredient w/c consist of(ingredient_id,name),category w/c consist of(category_id,name) and category_ingredients w/c consist of(ingredient_id,category_id). I create a form for adding many ingredients by category and i want to check if 1 or more ingredients already exist then i will only have to get the id's of the ingredient and then the other ingredients that don't exist will be inserted in my db. can u guys help me pls?

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

    $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)
        );
}
        //var_dump($joinData); die();
        $this->products_model->saveCategoryIngredients($joinData);

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

You just have to add a function to your model, like this :

<?php

public function getIngredientByName($name) {
    return $this->db
    ->from('ingredient I')
    ->where('I.name', $name)
    ->get()->row(); //Will return the row of ingredient if ingredient exists, else null
}

And in your controller :

<?php
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
    if (!$this->products_model->getIngredientByName($value)) {
        $saveData[] = array(
            'ingredient_id' => null,
            'name'  => trim($value)
        );  
    }
}

Thanks Gwendal for answering this question i am modifying this answer a bit to prevent duplicate inserts eg:- if user inserts SuGar CaNe but we have in our database Sugar Cane then the with answer's code we will have 2 sugar cane to avoid these type of inserts we can use this code for model

<?php

public function getIngredientByName($name) {
    return $this->db
    ->from('ingredient I')
    -> where("( REPLACE( LOWER(I.name), ' ', '') LIKE '".strtolower(preg_replace('/\s+/', '', $name)) ."%')")
    ->get()->row(); //Will return the row of ingredient if ingredient exists, else null
}

for controller same as

<?php
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
    if (!$this->products_model->getIngredientByName($value)) {
        $saveData[] = array(
            'ingredient_id' => null,
            'name'  => trim($value)
        );  
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM