example of listBox

Can anyone explain an example of selected data in listBox?

the html-code is




    <select name="top" size="5">

      <option selected>Beer</option>

      <option>Wine</option>

      <option selected>Water</option>

      <option>Gin</option>



thx for enlightenment

I think this should work

echo CHtml::dropDownList(‘top’, ,

          array('beer' =&gt; 'Beer', 'wine' =&gt; 'Wine','water'=&gt;'Water',..));

Yes, that work. But my problem is the selected areas. I have a default array (beer, wine, water) and selected products in a model (like beer and water). This would be visible as selected in a listBox of a form.

Okay, a working code:




// $model is a model for customer and bougth products

// $pmodel is a model for products for sale

echo $form->listBox($model,'products',CHtml::listData($pmodel,'product','description'),array('multiple'=>'multiple','size'=>'10','options'=>array(  'Beer'=>array('selected'=>'selected'), 'Wine'=>array('selected'=>'selected') )));



How can I select bought products from $model->products display in options?

My final solution:




// $model is a model for customer and bougth products

// product value in database is comma separeted (Beer,Wine,Water)

// $pmodel is a model for products for sale


                <?php echo $form->labelEx($model,'products'); ?>

                <?php

                        if(isset($model->products))

                        {

                                // snipe product list and build the option array

                                $m = explode(",", $model->products);

                                $x = 0;

                                foreach ($m as $p)

                                {

                                         $pp[$p] = array('selected'=>'selected');

                                         $x++;

                                }

                        }

                         echo $form->listBox($model,'products',CHtml::listData($pmodel,'product','description'),array('multiple'=>'multiple','size'=>count($pmodel),'options'=>$pp ));

                ?>

                <?php echo $form->error($model,'products'); ?>




HTH HAND

Thank you for this solution!

Regards,

JP

Hi,

check this wiki

http://www.yiiframework.com/wiki/681/update-related-models-of-a-model-using-listbox-and-checkboxlist-in-the-form/