Skip to content

Instantly share code, notes, and snippets.

@ninetwentyfour
Created August 28, 2011 18:39
Show Gist options
  • Save ninetwentyfour/1177028 to your computer and use it in GitHub Desktop.
Save ninetwentyfour/1177028 to your computer and use it in GitHub Desktop.
Create A Ghetto, But Functional, Search Function For CakePHP - blogpost
<div id="search">
<form id="orderform" action="<?php echo $html->url('/users/search'); ?>" method="post" enctype="multipart/form-data">
<label for="searchuser">Search Users:</label><input type="text" name="searchuser" />
Search By:
<input type="radio" name="searchtype" value="User.id" checked> User ID
<input type="radio" name="searchtype" value="User.email" checked> User Email
<input type="radio" name="searchtype" value="Entity.name"> Entity Name
<?php
echo $form->end('Search');
?>
</div>
<?php
function search() {
$searchtype = $_POST['searchtype'];
$value = $_POST['searchuser'];
$results = $this->User->find('all', array('fields' => array(
'User.id',
'User.email',
'Entity.name',
'User.created',
'User.modified'
),
'order' => 'User.id ASC',
'conditions' => array($searchtype . ' ' . 'LIKE' => '%'.$value.'%')
));
$this->set('results', $results);
}
?>
<div id="search">
<form id="orderform" action="<?php echo $html->url('/users/search'); ?>" method="post" enctype="multipart/form-data">
<label for="searchuser">Search Users:</label><input type="text" name="searchuser" />
Search By:
<input type="radio" name="searchtype" value="User.id" checked> User ID
<input type="radio" name="searchtype" value="User.email" checked> User Email
<input type="radio" name="searchtype" value="Entity.name"> Entity Name
<?php echo $form->end('Search'); ?>
</div>
<div id="contentbox">
<h2>Manage Users</h2>
<table>
<thead>
<th>ID</th><th>Email</th><th>Entity Name</th>
</thead>
<?php foreach($results as $user) : ?>
<tr>
<td><?php echo $user['User']['id'] ?></td>
<td><?php echo $user['User']['email'] ?></td>
<td><?php echo $user['Entity']['name'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment