Skip to main content
Topic: select filled from datatable (Read 1675 times) previous topic - next topic

select filled from datatable

following select was created by the optionListDataSource property of a form field:
<select id="ctrl-klant_gen" name="klant_gen" placeholder="Selecteer een waarde" class="custom-select">
 <option selected="">2</option>
 <option value="Casius">Casius</option>
 <option value="Werkspot">Werkspot</option>
</select>

This was the generated DataSource Query: ""SELECT  DISTINCT gen_id AS value,gen_naam AS label FROM generators ORDER BY gen_naam""

Only the DISTINCT checkbox was ticked.

Nothing wrong with the query, but how it is internally presented to create the select, must go wrong. The first (selected) option, is the present value of the field in the first record in the table. Shouldnt be in the select at all. Then the values equal the labels in option 2 and 3, which is not correct. This should have been the correct select:

<select id="ctrl-klant_gen" name="klant_gen" placeholder="Selecteer een waarde" class="custom-select">
 <option value="3">Casius</option>
 <option selected value="2">Werkspot</option>
</select>

Can you please fix and give me a workaround for the time being?

Re: select filled from datatable

Reply #1
To give a hint, in the custom page we find within the Select as first option:
<option selected><?php echo $data['klant_gen']; ?></option>
This causes the frist option: <option selected="">2</option>; I cant see a reason to have this line here, it should not be
Then in the loop for all the array content:
<?php
   $rec = $data['klant_gen'];
   $klant_gen_options = $comp_model -> klanten_klant_gen_option_list();
   if(!empty($klant_gen_options)){
   foreach($klant_gen_options as $arr){
         $val=array_values($arr);
         $selected = ( $val[0] == $rec ? 'selected' : null ) ;
         ?>
         <option <?php  echo $selected; ?> value="<?php echo $val[0]; ?>"><?php echo (!empty($val[1]) ? $val[1] : $val[0]); ?></option>

I think this is a kind of mix-up by using array_values($arr), you loose the value => label relation.
Lets see, in my table I have two pairs (ID,Value) for now: 1=>Casius  and 2=>Werkspot. What happens in this piece of code?
The val array will look like [0] => Casius, [1]-> Werkspot. so the line <option <?php  echo $selected; ?> value="<?php echo $val[0]; ?>"><?php echo (!empty($val[1]) ? $val[1] : $val[0]); ?></option> will end up in two options where value and label are equal and the original value from the database table is lost. I think you want to have a code for both 1 column and two colomn arrays. In this way you loose the intended relationship.

What i would like to have at this point, is a twodimensional array, derived from the database table :
1=>Casius; 2=>Werkspot.

Then the code to create the options would be :
foreach($array as $value => $label){
  <option <?php  echo $selected; ?> value="<?php echo $value; ?>"><?php echo $label; ?></option>
}
Which would give me the optionslisting.
So to workaround (though i really think this should be a standard field) is to have such an array.

Re: select filled from datatable

Reply #2
Oh Ooh, also the other options to fill the select are not ok! int the 'enter value' option, the is still that first line selected with no value and label 1