本文主要是介绍CakePHP v3.9.3 数据一致检索 模糊检索,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.Apache,MySQL start
2.
index.ctp
C:\xampp\htdocs\mycakeapp\src\Template\People
<p>This is People table records.</p>
<?=$this->Form->create(null,
['type'=>'post',
'url'=>['controller'=>'People',
'action'=>'index']]) ?>
<div>find</div>
<div><?=$this->Form->text('People.find') ?></div>
<div><?=$this->Form->submit('検索') ?></div>
<?=$this->Form->end() ?>
<hr>
<table>
<thead><tr>
<th>id</th><th>name</th><th>mail</th><th>age</th><th></th>
</tr></thead>
<?php foreach($data->toArray() as $obj): ?>
<tr>
<td><?=h($obj->id) ?></td>
<td><a href="<?=$this->Url->build(['controller'=>'People',
'action'=>'edit']); ?>?id=<?=$obj->id ?>">
<?=h($obj->name) ?></a></td>
<td><?=h($obj->mail) ?></td>
<td><?=h($obj->age) ?></td>
<td><a href="<?=$this->Url->build(['controller'=>'People',
'action'=>'delete']); ?>?id=<?=$obj->id ?>">delete</a></td>
</tr>
<?php endforeach; ?>
</table>
3.一致检索
PeopleController.php
<?php
namespace App\Controller;
use App\Controller\AppController;
class PeopleController extends AppController{
public function index() {
if ($this->request->is('post')){
$find = $this->request->data['People']['find'];
$condition = ['conditions'=>['name'=>$find]];
$data = $this->People->find('all', $condition);
} else {
$data = $this->People->find('all');
}
$this->set('data', $data);
}
}
?>
4.
http://localhost/mycakeapp/people/
5.模糊检索
PeopleController.php
C:\xampp\htdocs\mycakeapp\src\Controller
<?php
namespace App\Controller;
use App\Controller\AppController;
class PeopleController extends AppController{
public function index() {
if ($this->request->isPost()){
$find = $this->request->data['People']['find'];
$condition = ['conditions'=>['name like'=>$find]];
$data = $this->People->find('all', $condition);
} else {
$data = $this->People->find('all');
}
$this->set('data', $data);
}
}
?>
6.
http://localhost/mycakeapp/people
这篇关于CakePHP v3.9.3 数据一致检索 模糊检索的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!