Reputation: 131
Controller code:
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);
}
}
}
Form code:
<?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));
}
}
Model code:
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; ?>
Hi Im trying to do a search page but I kept getting the foreach() error. Did a var_dump and it says NULL. Wondering where I have gone wrong. Stuck at the same problem for hours before deciding to seek help from you guys.
Upvotes: 1
Views: 158
Reputation: 1243
you have a case where you dont give any value to $item. You may try add
ing:
} else {
//If the data is not valid, redisplay the information on the form so that
//the user can correct appropriately.
$form->populate($formData);
$item = array(); //add this here or before the if block
}
else add...
<?php if(!is_null($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; ?>
<?php } ?>
Upvotes: 2
Reputation: 706
in foreach try:
$myItems = $this->item;
<?php foreach($myItems as $item) : ?>
Upvotes: 0