php - Showing comma separated dropdown values from column in database -
i have saved multiple choices dropdown select menu database field , separate them comma. how can load them in dropdown menu again? saved as: choice1
,choice2
,choice55
...etc. query joining 2 tables , it's look's this:
$pdo = database::connect(); $sql="select t1.*, t2.* form_fields t1, user_choices t2 t1.field_name = t2.field_name , group_id=1 , user_id=".$_session['user_id']." echo '<select class="form-control" name="program">'; foreach ($pdo->query($sql) $row_program){ echo '<option value='.$row_program['field_name'].'>'.$row_program['field_name'].'</option>'; } echo '</select>';
so need display values user_choices t2
column program
. query showing result if there 1 value in column. when there multiple value saved comma showing error
php warning: invalid argument supplied foreach()...
use explode()
comma give array of options , set dynamic option values in dropdown.
<?php $pdo = database::connect(); $sql="select t1.*, t2.* form_fields t1, user_choices t2 t1.field_name = t2.field_name , group_id=1 , user_id=".$_session['user_id']." "; echo '<select class="form-control" name="program">'; foreach ($pdo->query($sql) $row_program){ $options = $row_program['field_name']; $optionsarr = explode(",", $options); foreach ($optionsarr $row){ echo '<option value='.$row.'>'.$row.'</option>'; } } echo '</select>'; ?>
Comments
Post a Comment