根据下拉列表中的选定值更改数据表值(CodeIgniter)

时间:2018-02-07 03:22:44

标签: php ajax codeigniter dropdown

所以我有一个显示数据库中所有课程的数据表,我想使用下拉列表中的值对数据进行排序(包含课程部门)我想根据所选部门对所有课程进行排序。例如,我选择了ICT部门,该部门的所有课程都将显示在数据表中。我该怎么做?

型号:

 public function getCoursesByDepartment($DepartmentID)
    {
        $this->dbi->select('course.CourseCode, course.CourseTitle');
        $this->dbi->from('course');
        $this->dbi->where('course.DepartmentID', $DepartmentID);
        $res = $this->dbi->get()->result();

        return $res;
    }



public function get_departments() { 
        $result = $this->dbi->select('DepartmentID, DepartmentName')->get('department')->result_array(); 

        $DepartmentID = array(); 
        foreach($result as $r) { 
            $DepartmentID[$r['DepartmentID']] = $r['DepartmentName']; 
        } 

        $DepartmentID[''] = 'Select Department...'; 
        return $DepartmentID; 
    } 

控制器:

 public function getcoursesbydepartment()
 {

        $this->load->model('Admin_model');
        $allcourses = $this->Admin_model->getCoursesByDepartment($DepartmentID);
        $data['courses'] = $allcourses;
        $this->load->view('Admin/managecourses_view', $data);


     }

 public function getcourses() 
{ 
    $data['DepartmentID'] = $this->Admin_model->get_departments(); 
    $this->load->view('Admin/addcourse_view', $data); 
} 

在我看来,我的下拉列表中填充了数据库中的值。 查看:

  <div class="card-pf-body" style="margin-top: 5px;">

      <form class="needs-validation" novalidate >


   <div class="form-row">
    <div class="col-md-4 mb-3">
      <label for="validationCustom03"  style="margin-top: 15px!important;margin-left: -20px!important">Sort by department:</label>

        <div class="panel-body" style="margin-left: -36px;margin-top: -15px;"> 
                <!--dropdown input--> 

       <!--HERE IS MY DROPDOWN (VALUES RETRIEVED FROM DATABASE)-->

        <?php echo form_dropdown('DepartmentID', $DepartmentID, '', 'class="form-control"', 'name="DepartmentID"', '#', 'id=departmentid', (isset($_POST['DepartmentID']) ? $_POST['DepartmentID'] : ''), 'id="DepartmentID"') ?>



            </div> 
    </div>    

     <div class="col-md-4 mb-3">

    </div>    
  </div>


   <p style="margin-top: 20px;margin-right: 1000px;margin-left: 5px;width: 80px;margin-bottom: 10px"><font style="color: #ffffff">p</font></p>

</form>

    <table class="table table-responsive table-bordered table-striped" id="example"> 
      <thead>
    <tr>
      <th>Course</th>
      <th>Title</th>
     <!--  <th>Description</th> -->

      <th>Option</th>
    </tr>
    </thead>
    <?php 
    $i = 0;
    foreach ($courses AS $course): ?>
    <tr>

      <td><?php echo $course->CourseCode; ?></td>
      <td><?php echo $course->CourseTitle; ?></td>

    <!--   <td><a href="<?php echo $course->CourseDescription; ?>">View Description</a></td> -->
      <!-- <td><?php echo $course->CourseDescription; ?></td> -->
      <td>
        <a href="#"" onClick="editcourse(<?php echo $course->CourseID;?>)">Edit<i class="fa fa-pencil" style="margin-left: 5px"></i></a>  

       <!--  <button class="btn btn-primary" onclick="editcourse(<?php echo $course->CourseID;?>)"><i class="glyphicon glyphicon-remove"></i></button> -->

        &nbsp; |    &nbsp;
       <a href="#" onClick="deletecourse(<?php echo $course->CourseID;?>)"><font style="color: #D2553D;">Delete</font><i class="fa fa-times" style="margin-left: 5px;color: #D2553D;"></i></a>

      </td>
    </tr>
   <?php endforeach; ?> 
    </table>
  </div>
  </div>
</div>



<script>
  $(document).ready(function() {
    // matchHeight the contents of each .card-pf and then the .card-pf itself
    $(".row-cards-pf > [class*='col'] > .card-pf .card-pf-title").matchHeight();
    $(".row-cards-pf > [class*='col'] > .card-pf > .card-pf-body").matchHeight();
    $(".row-cards-pf > [class*='col'] > .card-pf > .card-pf-footer").matchHeight();
    $(".row-cards-pf > [class*='col'] > .card-pf").matchHeight();

    // Initialize the vertical navigation
    $().setupVerticalNavigation(true);
  });
</script>

<script type="text/javascript">
   $(document).ready(function() {

    $('#example').DataTable();
} );
 </script>

1 个答案:

答案 0 :(得分:1)

基本上您想要使用所有选项呈现页面,然后在选择更改时,将用户重定向到具有其他网址属性的同一页面/somepage/{someid}

// [CONTROLLER]
// specific department: /getcoursesbydepartment/{dept_id}
// all departments:     /getcoursesbydepartment/
public function getcoursesbydepartment($DepartmentID = null) {
    $this->load->model('Admin_model');
    $data['departments'] = $this->Admin_model->get_departments();
    $data['courses'] = $this->Admin_model->getCoursesByDepartment($DepartmentID);
    $data['selected_department'] = $DepartmentID; // added
    $this->load->view('Admin/managecourses_view', $data);
}

这需要更改您的型号代码:

注意:我已添加,所以如果没有结果,函数返回null,这通常是一个好习惯(返回null,false或空array())。否则,如果没有行,则视图中的result()foreach将失败。

public function getCoursesByDepartment($DepartmentID = null) {
    $this->dbi->select('course.CourseCode, course.CourseTitle');
    $this->dbi->from('course');
    if (!is_null($DepartmentID)) {
        $this->dbi->where('course.DepartmentID', $DepartmentID);
    }
    $query = $this->dbi->get();
    if ($query->num_rows() < 1) {
        return null;
    }
    return $query->result();
}

您的下拉代码也看起来有些偏差,我修复了它并将所选部门添加为要选择的值。正如您将注意到的,如果未选择任何内容,则根据控制器代码,该值将为null

//https://www.codeigniter.com/userguide3/helpers/form_helper.html#form_dropdown
//form_dropdown([$name = ''[, $options = array()[, $selected = array()[, $extra = '']]]])
echo form_dropdown('DepartmentID', $departments, $selected_department, 'class="form-control" id="DepartmentID"');

$DepartmentID变量应该是一个类似于:

的数组

array('93'=>'HR', '21'=>'Marketing')其中的数字是部门ID(更新:看起来你正在这样做)。

现在你要做的就是使用js或jquery,选择更改重定向到: /getcoursesbydepartment/{dept_id}其中{dept_id}是选择框的值。