Php 根据下拉列表中的选定值更改数据表值(CodeIgniter)
因此,我有一个数据表,显示数据库中的所有课程,我想使用下拉列表中的值对数据进行排序,其中包含课程部门。我想根据所选的部门对所有课程进行排序。例如,我选择了ICT部门,该部门的所有课程都将显示在数据表上。我该怎么做 型号:Php 根据下拉列表中的选定值更改数据表值(CodeIgniter),php,ajax,codeigniter,dropdown,Php,Ajax,Codeigniter,Dropdown,因此,我有一个数据表,显示数据库中的所有课程,我想使用下拉列表中的值对数据进行排序,其中包含课程部门。我想根据所选的部门对所有课程进行排序。例如,我选择了ICT部门,该部门的所有课程都将显示在数据表上。我该怎么做 型号: public function getCoursesByDepartment($DepartmentID) { $this->dbi->select('course.CourseCode, course.CourseTitle');
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);
}
在我看来,我的下拉列表中填充了数据库中的值。
视图:
基本上,您希望使用所有选项呈现页面,然后在选择“更改”时,使用附加的url属性/somepage/{someid}将用户重定向到同一页面 这需要更改您的型号代码: 注意:我添加了这样一个选项,如果没有结果,函数将返回null,这通常是返回null、false或空数组的好方法。否则,如果没有行,视图中的result和foreach将失败 $DepartmentID变量应该是如下所示的数组: 数组'93'=>'HR','21'=>'Marketing',其中的数字是部门id的更新:看起来你做得对 现在,您所要做的就是,使用js或jquery,选择change redirect to(更改重定向到):
/getcoursesbydepartment/{dept_id}其中{dept_id}是选择框的值。在控制器中,哪里定义了$DepartmentID?我只是看到它被引用了。我编辑了代码:你能检查一下吗?我已经更新了我的答案以反映更改遇到的PHP错误严重性:注意消息:未定义变量:selected_department文件名:Admin/managecourses_view.PHP行号:1244为什么selected_department不被视图识别?你的控制器看起来完全正确吗像我的一样?e、 g.$data['selected_department']=$DepartmentID;公共函数getcoursesbydepartment$DepartmentID=null?此处部门id的值应该是多少$数据['selected_department']=$DepartmentID$DepartmentID来自url请参见:getcoursesbydepartment$DepartmentID=null用法:/getcoursesbydepartment/{dept_id}。。。当/getcoursesbydepartment/123时,部门id为123
<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> -->
|
<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>
// [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);
}
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();
}
//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"');