参数foreach(),NULL数组无效

时间:2012-08-09 12:23:41

标签: php zend-framework

控制器代码:

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。想知道我哪里出错了。在决定寻求你们的帮助之前,坚持同样的问题几个小时。

2 个答案:

答案 0 :(得分:2)

你有一个案例,你不给$ item任何价值。您可以尝试添加

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    
            }

否则添加......

<?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 } ?>

答案 1 :(得分:0)

在foreach尝试:

$myItems = $this->item;

<?php foreach($myItems as $item) : ?>