如何使用通配符搜索从CakePHP的脚手架中获取列表? [英] How can I use a wildcard search to get a list from scaffolding in a CakePHP?
本文介绍了如何使用通配符搜索从CakePHP的脚手架中获取列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经为CakePHP构建了一个脚手架,但是需要一种方法让用户在文本框中输入部分姓氏,单击一个按钮,然后将要过滤的人员列表过滤为通配符搜索。
I've got a scaffold built for CakePHP, but need to have a way for users to type in part of a surname into a text box, click a button and for the list of people to be filtered to a wildcard search.
我正在努力寻找示例代码。有人有吗?
I'm struggling for example code. Does anyone have some?
推荐答案
视图/用户/index.ctp
views/users/index.ctp
<fieldset>
<legend>Filter users</legend>
<?php
echo $form->create('User', array('action' => 'index'));
echo $form->input('surname', array('div' => false));
echo $form->submit('Filter', array('div' => false));
echo $form->end();
?>
</fieldset>
controllers / users_controller.php
controllers/users_controller.php
public function index($surname = null) {
// post-redirect-get
if ($this->data['User']['surname']) {
$this->redirect(array('id' => $this->data['User']['surname']));
}
// conditions
$conditions = null;
if ($surname) {
$conditions = array('surname LIKE' => '%' . $surname . '%');
}
// find
$users = $this->Users->find('all', array('conditions' => $conditions));
// set
$this->set(compact('users'));
}
这篇关于如何使用通配符搜索从CakePHP的脚手架中获取列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文