簡體   English   中英

CODEIGNITER:檢查成分是否存在

[英]CODEIGNITER: check if ingredient exist

我有表成分w / c包含(ingredient_id,name),類別w / c包含(category_id,name)和category_ingredients w / c包含(ingredient_id,category_id)。 我創建了一個用於按類別添加許多成分的表單,我想檢查是否已存在一種或多種成分,那么我只需要獲取成分的ID,然后將其他不存在的成分插入到我的數據庫中。 你們能幫我嗎?

這是我的代碼: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(); ?>

控制器:

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


}

模型:

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

您只需要向模型中添加一個函數,就像這樣:

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

在您的控制器中:

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

感謝Gwendal回答了這個問題,我正在對此答案進行一些修改,以防止重復插入,例如:-如果用戶插入SuGar CaNe,但我們在數據庫中有甘蔗,那么帶有答案的代碼中我們將有2個甘蔗來避免此類插入我們可以使用此代碼進行模型

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

與控制器相同

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM