I have a problem, I want to display all rows from database to dynamic onchange select form, but it displays only first or only last row. Could you please help me someone?
this is my code:
<?php
$selected11 = '';
function get_options11($vyber11) {
$queryyy = mysql_query("SELECT funkcia, ID_funk FROM funkcie");
while($row = mysql_fetch_array($queryyy)) {
$v11=$row['ID_funk'];
$k11=$row['funkcia'];
$moznosti11 = '';
if($vyber11==$v11){
$moznosti11.='<option value="'.$v11.'" selected>'.$k11.'</option>';
} else{
$moznosti11.='<option value="'.$v11.'">'.$k11.'</option>';}
}
return $moznosti11;
}
if(isset($_POST['funkcia'])) {
$selected11 = $_POST['funkcia'];
}
}
?>
<select name="funkcia" class="ramceky" style="width: 150px; height: 40px" onchange="this.form.submit();">
<option><?php echo get_options11($selected11); ?></option>
</select>
You are assigning the variable $moznosti11;
in every loop. Move it right before while loop and it should work like charm.
See here:
$moznosti11 = '';
while($row = mysql_fetch_array($queryyy)) {
$v11=$row['ID_funk'];
$k11=$row['funkcia'];
if($vyber11==$v11){
$moznosti11.='<option value="'.$v11.'" selected>'.$k11.'</option>';
} else{
$moznosti11.='<option value="'.$v11.'">'.$k11.'</option>';}
}
return $moznosti11;
}
The problem is you return with options, into one <option>
.
Change this:
<option><?php echo get_options11($selected11); ?></option>
to this:
<?php echo get_options11($selected11); ?>