1

Here i working custom form with custom select box in magento.I need to assign Custom select box with option value attributes and option values .

Here i working custom form sample code:

$fabrics    = Mage::getModel('magentostudy_fabrics/fabrics')->getCollection(); 
        $fabricval[] = "Please Select";
        $fabricvals[] = "0";
        // $collection->addAttributeToSelect('news_id'); //retrieve only product name (optimising SQL)
        foreach ($fabrics as $fabric) //loop for getting products
        {
            $fabricval[] = $fabric->getTitle(); 
            $fabricvals[] = $fabric->getNews_id(); 
        }
 $fieldset->addField('fabricslist', 'select', array(
            'name'     => 'fabricslist',
            'label'    => Mage::helper('bulk_uploader')->__('Select Fabrics List'),
            'title'    => Mage::helper('bulk_uploader')->__('Select Fabrics List'),
            'required' => true,
            'onclick' => "",
            'onchange' => "",
            'value'  => $fabricvals,
            'values'    =>$fabricval,
            'disabled' => $isElementDisabled,
            'readonly' => false,
            'after_element_html' => '<small>choose Category list</small>',
            'tabindex' => 1   
        ));

Here 'value' => $fabricval got wrong values.For example i have News_ID 1,3,5 respectively.But i results only 1,2,3 respectively.How to assign value attributes in select box for example I need select box sample function

<select>
<option value='1'>sample1</option>
<option value='3'>sample2</option>
<option value='5'>sample3</option>
</select>

How to assign select box option value attributes in magento?

1 Answer 1

2

HereBy, Finally i found solution based on option value assignment with auto-increment value.

Simple adjustment in above code it return proper results

  $fabrics    = Mage::getModel('magentostudy_fabrics/fabrics')->getCollection(); 
  $fabricval[] = "Please Select";
  $fabricvals[] = "0";
  foreach ($fabrics as $fabric) {
     $id =$fabric->getNews_id();
     $fabricval[$id] = $fabric->getTitle(); 
  }

Here i made some change in my code, I get auto-increment id($id =$fabric->getNews_id();$fabricval[$id] = $fabric->getTitle();) and assign into array key value and place into custom form like this

$fieldset->addField('fabricslist', 'select', array(
            'name'     => 'fabricslist',
            'label'    => Mage::helper('bulk_uploader')->__('Select Fabrics List'),
            'title'    => Mage::helper('bulk_uploader')->__('Select Fabrics List'),
            'required' => true,
            'onclick' => "",
            'onchange' => "",
            'values'    =>$fabricval,
            'disabled' => $isElementDisabled,
            'readonly' => false,
            'after_element_html' => '<small>choose Category list</small>',
            'tabindex' => 1   
        ));

This results properly what i need

<select>
<option value='1'>sample1</option>
<option value='3'>sample2</option>
<option value='5'>sample3</option>
</select>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.