简体   繁体   中英

How to pass a result in phtml file from controller Magento?

I am submitting a form and getting result from model.I want to display the result in phtml file ie the whole collection after submitting need to be in phtml file

Here is my code

Controller

        $this->loadLayout();

    if($this->getRequest()->isPost())
    { 
        $post = $this->getRequest()->getPost();
        $data = array();
        $collection = Mage::getModel('searchservice/service')->getCollection();
        $collection->addFieldToFilter('compstate',array('eq'=>$post['input1']));

        Mage::getSingleton('core/session')->addSuccess(Mage::helper('searchservice')->__('Displaying Your Result'));
        $this->renderLayout();

    }

I want data in an aaray returned from collection filter and use in phtml with foreach.

you can use magento registry for setting and getting values as:

set a value (in controller)

Mage::register('var_name',$var_value);

get a value (in phtml/view)

$var_value = Mage::registry('var_name');

more you can find at

http://alanstorm.com/magento_registry_singleton_tutorial
https://stackoverflow.com/a/18157176/725306

Here is the solution i found in Controller:

Mage::register('data', $collection);

in view

$data = Mage::registry('data');

so the code becomes

        if($this->getRequest()->isPost())
    { 
        $post = $this->getRequest()->getPost();
        $data = array();
        $collection = Mage::getModel('searchservice/service')->getCollection();
        $collection->addFieldToFilter('compstate',array('eq'=>$post['input1']));
        foreach ($collection as $record) 
        {
            $data[] = $record->getProfileurl();
        }
        Mage::register('data', $data);
        Mage::getSingleton('core/session')->addSuccess(Mage::helper('searchservice')->__('Displaying Your Result'));
        $this->renderLayout();

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM