提交表单时,由数据库错误生成的codeigniter选择列表

时间:2015-03-10 12:44:08

标签: codeigniter

我创建了一个有两个选择框的表单,它也可以从数据库中生成选项,但问题是当我单击提交按钮时,选择框变为空。

这是代码:

查看:

    <ol class="breadcrumb-icon">
        <li><a href="<?php echo base_url(); ?>admin/home"><i class="fa fa-home"></i> Home</a></li>
        <li class="active"><a href="#"><i class="icon fa fa-inbox"></i> Add Courses</a></li>
    </ol>


    <div class="page-header">
        <h1>Add Courses:</h1>
    </div>

    <div class="well">
                <?php if(isset($error)) echo $error;?>
                    <?php
                        $this->load->helper('form');

                        $attributes = array('role' => 'form','class'=>'form-horizontal');
                        echo form_open_multipart('admin/addcourses',$attributes);
                    ?>
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">Course Category:</label>
                <div class="col-sm-10">
                    <select name="category" class="form-control">
                                                <?php
                                                    foreach ($cat as $row)
                                                        {
                                                           echo "<option value='".$row->category_id."'>".$row->category_name."</option>";
                                                        }
                                                ?>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">Course Location:</label>
                <div class="col-sm-10">
                    <select name="location" class="form-control">
                        <?php
                                                    foreach ($loc as $row)
                                                        {
                                                           echo "<option value='".$row->location_id."'>".$row->location_name."</option>";
                                                        }
                                                ?>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">Course Language:</label>
                <div class="col-sm-10">
                    <select name="language" class="form-control">
                        <option value="English">English</option>
                        <option value="Arabic">Arabic</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">Course Name</label>
                <div class="col-sm-10">
                <input type="text" name="coursename" class="form-control" id="inputPassword3" placeholder="">
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">Course Date:</label>
                <div class="col-sm-10">
                    <input type="text" name="date" id="range-picker" readonly class="form-control kd-daterange">
                </div>
            </div>

            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">Course Image</label>
                <div class="col-sm-10">
                    <input type="file" name="course_image" class="upload" readonly class="form-control kd-upload">
                    <p>keep it empty to use the default image.</p>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-default">Add Course</button>
                </div>
            </div>

        </form>
    </div>

</div> <!-- /.container laaast -->

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>

<script src="js/extended.min.js"></script>
<script>
    extended = new Extended();
</script>

Controller addcourses.php

public function addcourses()
    {
            if ($this->session->userdata('is_logged_in')) 
                {

                $this->load->model('admin/read_cat_loc');
                $data['cat']= $this->read_cat_loc->read_categories();
                $data['loc']= $this->read_cat_loc->read_locations();

                $this->load->view('admin/header_view');
                $this->load->view('admin/addcourses_view',$data);
                $this->load->view('admin/footer_view');
                }
            else {
                 redirect('admin/home');
                }
    }

型号代码

类Read_cat_loc扩展了CI_Model {

public function read_categories() {

    $query= $this->db->get('course_category');

    return $query->result();
}


public function read_locations() {

    $query= $this->db->get('course_location');

    return $query->result();


}

}

1 个答案:

答案 0 :(得分:0)

好的解决了:

我在主控制器部分做了这个:

            $this->load->model('admin/read_cat_loc');
            $data['cat']= $this->read_cat_loc->read_categories();
            $data['loc']= $this->read_cat_loc->read_locations();

            $this->load->view('admin/header_view');
            $this->load->view('admin/addcourses_view',$data);
            $this->load->view('admin/footer_view');

但在我必须重新加载视图的其他区域我只做了这个:

            $this->load->view('admin/header_view');
            $this->load->view('admin/addcourses_view',$data);
            $this->load->view('admin/footer_view');

没有模型部分再次保留数据......

如果你知道更好的方式它会很棒。感谢