控制器代码:
public function searchAction() //search action for items
{
$form = new Application_Form_Search(); //pass through search form
$form->submit->setLabel('Search Item!'); //set submit button as 'Search item!'
$this->view->form = $form; //pass to search.phtml
if ($this->getRequest()->isPost()) { //Check if 'Submit' button is clicked
$formData = $this->getRequest()->getPost(); //Checking the values from the 'Submit' button
if ($form->isValid($formData)) { //Check if form is valid
$item = new Application_Model_Item($form->getValues()); //Plug in values into item object
$db = new Application_Model_DbTable_Item(); //Create an item object for DbTable_Item
$db->searchItem($item->getName()); //search item in DbTable_Item via the searchItem
$this->_helper->redirector->gotoSimple('search', 'item' );
} else {
//If the data is not valid, redisplay the information on the form so that
//the user can correct appropriately.
$form->populate($formData);
}
}
}
表格代码:
<?php
class Application_Form_Search extends Zend_Form
{
public function init()
{
$this->setName('search');
$search = new Zend_Form_Element_Submit('submit');
$search->setLabel('Search');
$search->setAttrib('itemname', 'submitbutton');
$itemname = new Zend_Form_Element_Text('itemname'); //create text box for stock
$itemname->setLabel('Search Item Name:')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('Alpha')
->addValidator('NotEmpty');
//$list->setLabel('List');
//remember to add the declare form elements to form
$this->addElements(array($itemname, $search));
}
}
型号代码:
public function searchItem($itemname) //search item based on itemname
{
$itemname = (string)$itemname; //let itemid to be integer
// $sql = 'SELECT * FROM item WHERE `itemname` LIKE ?';
//$row = $this->fetchRow('itemname LIKE % . $itemname . %'); //find Row based on itemid
//$row = $this->fetchRow($sql, '%'.$itemname.'%');
$select = $this->select() //select from usertable and memberdetail
->from(array('item')) //join memberdetail and usertable through memberid = username
->where('itemname LIKE "%?%"', $itemname);
$row = $this->fetchAll($select);
if (!$row) { //if row can't be found
throw new Exception("Could not find row $itemname"); //Catch exception where itemid is not found
}
return $row->toArray();
}
foreach:
<?php var_dump($this->item); ?>
<?php foreach($this->item as $item) : ?>
<?php echo $this->escape($item['itemid']);?>
<?php echo $this->escape($item['image']);?>
<?php echo $this->escape($item['itemname']);?>
<?php echo $this->escape($item['description']);?>
<?php echo $this->escape($item['itemtype']);?>
<?php endforeach; ?>
嗨我试图做一个搜索页面,但我一直收到 foreach() 错误。做了一个var_dump,它说NULL。想知道我哪里出错了。在决定向你们寻求帮助之前,他们在同一个问题上停留了几个小时。