CODEIGNITER:检查是否存在

时间:2016-02-24 04:30:07

标签: php codeigniter exists

我的表格成分w / c由(ingredient_id,name)组成,w / c类别由(category_id,name)和category_ingredients组成,w / c由(ingredient_id,category_id)组成。我创建了一个表格,用于按类别添加许多成分,并检查该类别中是否已存在1种或更多成分,然后我只需要获取成分的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)
        {
            if (!$this->products_model->getIngredientByName($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:

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 getIngredientByName($name) {
    $this->db->select('ingredient_id');
    $this->db->limit(1);
    $this->db->where('category_id', $category_id);
    $ingredientQuery = $this->db->get('category_ingredient');
    $ingredient_id = $ingredientQuery->row()->ingredient_id;

    $this->db->select('name');
    $this->db->where('ingredient_id', $ingredient_id);
    $query = $this->db->get('ingredient');
    foreach($query->result() as $row)
    {
        return $row->name;
    }
}

0 个答案:

没有答案