php Codeigniter:过滤和搜索表格的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38973971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter: Correct way to filter and search tables
提问by Jethro Hazelhurst
problem
问题
I have tables of teachers, students and classes, this is what the students table looks like.
我有老师、学生和班级的表格,这就是学生表格的样子。
I need to be able to search through and filter the table to find the students. There will be about 100 students to sort through.
我需要能够搜索和过滤表格以找到学生。大约有100名学生需要整理。
I have a MVP that works but I know it is a bad way of solving the problem.
我有一个可行的 MVP,但我知道这是解决问题的糟糕方法。
What I have tried
我试过的
The flow goes like this...
流程是这样的……
By default when someone lands on the list of students allstudents are displayed.
However when someone enters a search and clicks on the 'Go' the form is submitted back to the same page... where the values are caught in an if/else statement.
If the fields are set then a different method in the model is called, which takes two parameters,
$field
(the database column name to search by) and$search
the string that we want to partially match.
默认情况下,当有人进入学生列表时,会显示所有学生。
但是,当有人输入搜索并单击“开始”时,表单将被提交回同一页面......其中的值被捕获在 if/else 语句中。
如果设置了字段,则调用模型中的不同方法,该方法需要两个参数
$field
(要搜索的数据库列名)和$search
我们想要部分匹配的字符串。
Here is the search form
这是搜索表单
<form class="form-inline" action="<?php echo base_url() . 'admin/students'; ?>" method="post">
<select class="form-control" name="field">
<option selected="selected" disabled="disabled" value="">Filter By</option>
<option value="first_name">First Name</option>
<option value="last_name">Last Name</option>
<option value="email">Email Name</option>
</select>
<input class="form-control" type="text" name="search" value="" placeholder="Search...">
<input class="btn btn-default" type="submit" name="filter" value="Go">
</form>
Which submits to the Students Controller
提交给学生控制器
public function index()
{
$filter = $this->input->post('filter');
$field = $this->input->post('field');
$search = $this->input->post('search');
if (isset($filter) && !empty($search)) {
$this->load->model('students/Student_Model');
$data['students'] = $this->Student_Model->getStudentsWhereLike($field, $search);
} else {
$this->load->model('students/Student_Model');
$data['students'] = $this->Student_Model->getStudents();
}
$data['module'] = 'admin';
$data['view_file'] = 'students/view';
$this->load->module('templates');
$this->templates->admin($data);
}
Which calls the in the getStudentsWhereLike()
method in the Student_Model
其中调用getStudentsWhereLike()
Student_Model中的方法
public function getStudentsWhereLike($field, $search)
{
$sql = "
SELECT * FROM students
WHERE $field LIKE '%$search%'
ORDER BY `students`.`registered_at`
";
$query = $this->db->query($sql);
return $query->result();
}
Question
题
Is this the right way to approach the issue? I know that the $this->db->query($sql)
method is open to SQL attacks so I will sanitize input, but I am interested if there is a better, more flexible, standardized approach to filtering/searching tables with Codeigniter.
这是解决问题的正确方法吗?我知道该$this->db->query($sql)
方法对 SQL 攻击是开放的,所以我会清理输入,但我很感兴趣是否有更好、更灵活、标准化的方法来使用 Codeigniter 过滤/搜索表。
回答by Drkdra
Use Codeigniter's active recordinstead:
改用 Codeigniter 的活动记录:
public function getStudentsWhereLike($field, $search)
{
$query = $this->db->like($field, $search)->orderBy('registered_at')->get('students');
return $query->result();
}