donglu6303 2016-05-16 17:47
浏览 30

为codeigniter添加编辑删除[关闭]

please help me work my edit and delete function.

here's my CONTROLLER:

class Recipe_controller extends CI_Controller{

    function __construct(){
        parent:: __construct();

        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('date');
        $this->load->helper('html');
        $this->load->database('recipedb');
        $this->load->library('form_validation');
        $this->load->model('Recipe_model');
    }

    function index(){
        $this->data['recipesview'] = $this->Recipe_model->get_recipe();
        $this->data['featuredday'] = $this->Recipe_model->get_featuredday();
        $this->load->view('recipehome_view', $this->data);
    }

    function submit_recipe(){
        $data['category'] = $this->Recipe_model->get_category();

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'png|jpg|gif';
        $config['max_size'] = '150';
        $config['max_width'] = '1024'; /* max width of the image file */
        $config['max_height'] = '768';

        $this->form_validation->set_rules('category', 'Category', 'callback_combo_check');
        $this->form_validation->set_rules('recipename', 'Recipe Name', 'required');
        $this->form_validation->set_rules('ingredient', 'Ingredient', 'required');
        $this->form_validation->set_rules('direction', 'Direction', 'require');
        $this->form_validation->set_rules('image', 'Image', 'required');
        $this->form_validation->set_rules('time', 'Time', 'required');
        $this->form_validation->set_rules('date', 'date', 'require');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('submitrecipe_view', $data);
        }
        else{
            $data = array(
                'category_id'   => $this->input->post('category'),
                'recipe_name'       => $this->input->post('recipename'),
                'ingredient'        => $this->input->post('ingredient'),
                'direction'         => $this->input->post('direction'),
                'image'             => $this->input->post('image'),
                'time'              => $this->input->post('time'),
                'date'              => date('Y-m-d', now($this->input->post('date'))));

            $upload_data = $this->upload->data();
            $this->db->insert('recipe', $data);
            $this->session->set_flashdata('msg', 'successful');
            redirect('recipe/submit');
        }
    }

    function combo_check($str){
        if ($str == '-SELECT-'){
            $this->form_validation->set_message('combo_check', 'Valid %s Name is required');
            return FALSE;
        }else{
            return TRUE;
        }
    }

    function edit_recipe($recipeid){

        $recipeid = $this->uri->segment(3);

        $data['category'] = $this->Recipe_model->get_category();
        $data['reciperecord'] = $this->Recipe_model->getById($recipeid);

        $this->form_validation->set_rules('category', 'Category', 'callback_combo_check');
        $this->form_validation->set_rules('recipename', 'Recipe Name', 'required');
        $this->form_validation->set_rules('ingredient', 'Ingredient', 'required');
        $this->form_validation->set_rules('direction', 'Direction', 'require');
        $this->form_validation->set_rules('time', 'Time', 'required');
        $this->form_validation->set_rules('date', 'date', 'require');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('editrecipe_view', $data);
        }
        else{

            //{

                $data = array(
                'category_id'       => $this->input->post('category'),
                'recipe_name'       => $this->input->post('recipename'),
                'ingredient'        => $this->input->post('ingredient'),
                'direction'         => $this->input->post('direction'),
                'image'             => $this->input->post('image'),
                'time'              => $this->input->post('time'),
                'date'              => date('Y-m-d', now($this->input->post('date'))));


                $this->db->where('recipe_id', $recipeid);
                $this->db->update('recipe', $data);
                $this->session->set_flashdata('msg', 'successful');
                redirect('recipe/edit' . $recipeid, $data);
            //}
        }
    }

    function view_appetizer(){
        $this->data['appetizer'] = $this->Recipe_model->get_appetizer();
        $this->load->view('appetizer_view', $this->data);

    }

}

MODEL:

class Recipe_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    function getById($recipeid){
        $query = $this->db->get_where('recipe', array('recipe_id' => $recipeid));
        return $query->row_array();
    }

    function get_category(){
        $this->db->select('category_id');
        $this->db->select('category_name');
        $this->db->from('recipe_category');
        $query = $this->db->get();
        $result = $query->result();

        $cat_id = array ('-Select-');
        $cat_name = array ('-Select-');

        for($i = 0; $i < count($result); $i++){
            array_push($cat_id, $result[$i]->category_id);
            array_push($cat_name, $result[$i]->category_name);
        }
        return $category_result = array_combine($cat_id, $cat_name);
    }

    function get_recipe(){
        $this->db->select('recipe_id, recipe_name, ingredient, direction');
        $this->db->from('recipe');
        $this->db->order_by('date', 'asc');
        $this->db->limit(3);
        $query = $this->db->get();

        return $query->result();
    }

    function get_featuredday(){
        $this->db->select('recipe_name');
        $this->db->from('recipe');
        $this->db->order_by('recipe_id', 'RANDOM');
        $this->db->limit(1);
        $query = $this->db->get();

        return $query->result();
    }

    function get_appetizer(){
        $this->db->select('recipe_id, recipe_name, ingredient, direction');
        $query = $this->db->get_where('recipe',array('category_id'=>1));
        return $query->result();
    }

}

and this is my VIEW for edit:

<h1>Edit Recipe Details</h1>
<?php 

        $attributes = array("class" => "form-horizontal", "id" => "recipeform", "name" => "recipeform", "method" => "post");
        echo form_open('recipe/edit/' .$recipeid, $attributes);
        ?>


                <label for="category" class="control-label">Category</label>
                <?php
                $attributes = 'class = "form-control" id = "category"';
                echo form_dropdown('category',$category,set_value('category', $reciperecord['category_id']), $attributes);?>
                <span class="text-danger"><?php echo form_error('category'); ?></span>
                <br/>
                <br/>

                <label for="recipename" class="control-label">Recipe Name</label>
                <?php
               // $attributes = 'class = "form-control" id = "recipename"';
                echo form_input(array(
                            'name'          => 'recipename', 
                            'id'            => 'recipename',
                            'value'         => 'recipename', $reciperecord['recipe_name'])); ?>
                <span class="text-danger"><?php echo form_error('recipename'); ?></span>
                <br/>
                <br/>

                <label for="ingredient" class="control-label">Ingredient</label>
                <?php
                $attributes = 'class = "form-control" id = "ingredient"';
                echo form_textarea(array(
                                'name'       => 'ingredient', 
                                'id'         => 'ingredient',
                                'rows'       => '10',
                                'cols'       => '50',
                                'value'      => 'ingredient', $reciperecord['ingredient'])); ?>
                <span class="text-danger"><?php echo form_error('ingredient'); ?></span>
                <br/>
                <br/>

                <label for="direction" class="control-label">Direction</label>
                <?php
                $attributes = 'class = "form-control" id = "direction"';
                echo form_textarea(array(
                                'name'       => 'direction', 
                                'id'         => 'direction',
                                'rows'       => '10',
                                'cols'       => '50',
                                'value'      => 'direction', $reciperecord['direction'])); ?>
                <span class="text-danger"><?php echo form_error('direction'); ?></span>
                <br/>
                <br/>

                <label for="time" class="control-label">Time</label>
                <?php
                $attributes = 'class = "form-control" id = "time"';
                echo form_input(array(
                            'name'          => 'time', 
                            'id'            => 'time',
                            'value'         => 'time', $reciperecord['time'])); ?>
                <span class="text-danger"><?php echo form_error('time'); ?></span>
                <br/>
                <br/>

                <label for="date" class="control-label">Date</label>
                <?php
                $attributes = 'class = "form-control" id = "date"';
                echo form_input(array(
                            'name'          => 'date', 
                            'id'            => 'date',
                            'value'         => 'date', $reciperecord['date'])); ?>
                <span class="text-danger"><?php echo form_error('date'); ?></span>
                <br/>
                <br/>


                <input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Insert" />
                <input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-danger" value="Cancel" />


        <?php echo form_close(); ?>
        <?php echo $this->session->flashdata('msg'); ?>
    </div>

    <?php include 'footer.php' ;?>
</body>

What is the possible wrong in my code for edit function and what did i forgot to put in my delete function? And how can i upload a picture saving it in a folder and the picture filename will be saved in the database? I hope someone might be able to help me I'm new in codeigniter. Thank you

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误