Created
August 28, 2011 18:39
-
-
Save ninetwentyfour/1177028 to your computer and use it in GitHub Desktop.
Create A Ghetto, But Functional, Search Function For CakePHP - blogpost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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