Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:29:10+00:00 2026-06-09T11:29:10+00:00

Controller code: public function searchAction() //search action for items { $form = new Application_Form_Search();

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:29:12+00:00Added an answer on June 9, 2026 at 11:29 am

    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 } ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See the following code of the <?php class DefaultController extends Controller { public function
how do i pass a multi diminutional array to a view? controller code public
I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string
$(#frmCompose).submit(function () { $(this).ajaxSubmit({ success: function (response) { alert('success'); } }); }); Controller code:
Inside my controller's action I have the following code: public ActionResult GridAction(string id) {
i m working classifieds controller this is code public function create_add(){ if(!empty($this->data)) { if($this->Classified->save($this->data)){
So here is my code - Controller public function addCategory() { if($this->session->userdata('logged_in') == TRUE)
I have the controller code as follows: $form_company=new Project_Form_AddCompany(); $this->view->form=$form_company; $form_company->setAction('add-Company'); if ($this->_request->isPost()) {
Here is the first part of my controller code: public class ControlMController : Controller
I'm passing from the controller an array generated by the next code: public ActionResult

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.