First of all I have a Model user.php which connects to users table. I have a controller UsersController.
I created a view for search: (filename: index.ctp)
<p><?php
echo $this->Form->create("Users", array('action' => 'search'));
echo $this->Form->input("Search Label", array('action' => 'search', 'name' => 'txt_search'));
echo $this->Form->end("Search");
?></p>
And this will go to UsersController/search() function
function search() {
if (!empty($this->data))
{
$name = $this->data['Users']['txt_search'];
$conditions = array("User.name Like " => "%$name%");
$result 开发者_StackOverflow中文版= $this->User->find('all', array('conditions'=> $conditions));
$this->set('users', $result);
}
}
And this will load search.ctp
My problem is, when I use the variable $users in search.ctp, it gives me an error Undefined variable: Users [APP\views\users\search.ctp, line 10].
I don't understand. Please help. Thanks!
You're specifying a custom name for your input and then you're checking $this->data
which will be empty because you're input is not named properly (and does not get auto populated in $this->data
). Use the following.
echo $this->Form->input("txt_search", array('label' => 'Search Label'));
A couple of things you should be looking at.
Set a default value to your
users
variable so your page doesn't break if they request it directly. Have$result = array();
at the top and do anempty()
check on it in search.ctpWhy have you specified an action attribute in your input? You don't need that.
精彩评论