For a module I was working on in one of my project, I needed to display a select box, with optgroup, Magento has inbuilt function for this.
Kindly go through this post Select Box in Magento of mine for understanding how to generate select box before reading forward.
For option group you need to pass the ‘value’ also as an array.
Suppose your Category name is ‘Furniture’ and there are two products ‘Chair’,’Couch’.
So you need to generate the array as shown here
$options = array( 0=> array( 'label' => 'Furniture', 'value' => array ( 0 => array ( 'label' => 'Chair', 'value' => 1 ), 1 => array ( 'label' => 'Couch', 'value' => 2 ) ) ) ); $tmp = Mage::app()->getLayout()->createBlock('core/html_select') ->setName('product_id') ->setId('product_id') ->setTitle('products') ->setClass('validate-select'); $select = $tmp->setOptions($options); echo $select->getHtml();
Hope this is helpful.