Add Multiselect field in Magento Admin Form

Add following code into _prepareForm() function in Form.php file.

$fieldset->addField(‘news_id’, ‘multiselect’, array(
‘label’     => Mage::helper(‘designer’)->__(‘News’),
‘required’  => false,
‘style’      => ‘width:400px’,
‘name’      => ‘news_id’,
‘values’    => Mage::getSingleton(‘designer/news’)->getNewsAll(),
));

Create new file News.php in Model folder and add following code into this file.

class Namespace_Designer_Model_News extends Mage_Core_Model_Abstract{

public function getNewsAll(){
// $news1 = Mage::getModel(‘news/state’)->getCollection();
$result = array();
$news = Mage::getModel(‘news/state’)->getCollection();
foreach ($news as $news1)
{
$addressLabel = $news1->getTitle();
//if (($addressLabel = $news1->getTitle()))
$result[] = array(‘value’=>$news1->getId(),’label’=>$addressLabel);
}

return $result;

}
}

Add following code into saveAction() function in DesignerController.php file.

$news_id1 = $data[‘news_id’];
$news_id = implode(“,”, $news_id1);

Set this $news_id in $model collection.

For Example,

$model = Mage::getModel(‘designer/designer’);
$model->setData($data)->setDesignerOptionId($newOption[‘option_id’])->setDesignerId($newOption[‘option_id’])->setNewsId($news_id);

Now, Convert these news ids into array by following code.

For Example,

$latest_news_id = $designers[‘news_id’];

if (isset($latest_news_id)) {
$latest_news_id = explode(‘,’,$latest_news_id);
if (is_array($latest_news_id)) {
$latest_news_id = array_unique($latest_news_id);
}
}
$news1 = Mage::getModel(‘news/state’)->getCollection()->addFieldToFilter(‘id’, array(‘in’ => $latest_news_id))->setOrder(‘id’,’desc’);
$news1->getSelect()->limit(3)->order(‘rand()’);

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: