Friends, I’m trying to adapt this tutorial for my project in cakephp 3.8. I have a user table and I want to do a search by name.
After generating the code with the bake and inserting the search field, my index.ctp looks like this:
<html>
<body>
<?= $this->Form->create('', ['type' => 'get']); ?>
<?= $this->Form->control('keyword', array('class' => 'auto','name'=> 'search','value' => '','label' => false,'required' => true));?>
<?= $this->Form->button(__('Search')); ?>
<?= $this->Form->end(); ?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('New User'), ['action' => 'add']) ?></li>
</ul>
</nav>
<div class="large-9 medium-8 columns content">
<h3><?= __('Users') ?></h3>
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('name') ?></th>
<th scope="col"><?= $this->Paginator->sort('created') ?></th>
<th scope="col" class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?= $this->Number->format($user->id) ?></td>
<td><?= h($user->nome) ?></td>
<td><?= h($user->created) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?>
</p>
</div>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
$(".auto").autocomplete({
source: "search.php",
minLength: 1
});
});
</script>
</body>
</html>
Here in this part, I don’t know how to adapt.
//autocomplete
$(".auto").autocomplete({
source: "/users",
minLength: 1
});
Here in this part, I don’t know how I could adapt.
My UsersController.php looked like this:
public function index()
{
$users= $this->paginate = [
'conditions' => [
'name like' => '%'. $this->request->getQuery('keyword') . '%',
]
];
$keyword = $this->Users->find('all');
$this->set('users', $this->paginate($keyword));
}
At the moment, for any search I get “No search results.”, Even if the name exists in the database.
I appreciate if anyone can analyze. Any comment is welcome!