代码之家  ›  专栏  ›  技术社区  ›  MSE

CODEIGNITER:检查成分是否存在

  •  0
  • MSE  · 技术社区  · 8 年前

    我有表成分w/c由(ingredient_id,name)组成,类别w/c包括(category_id,names),类别_indients w/c包含(ingredeent_id、category_ id)。我创建了一个按类别添加许多成分的表单,我想检查是否已经存在一种或多种成分,然后我只需要获取成分的id,然后将不存在的其他成分插入我的数据库中。你们能帮我吗?

    这是我的代码: 视图:

        <?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;}
    }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Gwendal    8 年前

    您只需向模型中添加一个函数,如下所示:

    <?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)
            );  
        }
    }
    
        2
  •  0
  •   Praveen Kumar    8 年前

    感谢Gwendal回答这个问题,我正在修改这个答案,以防止重复插入,例如:-如果用户插入 糖类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)
            );  
        }
    }