0

im create dynamic combo from databases and wanna show selected data after in edit menu this my code

<select class="form-control" id="id_prog" name="id_prog"  placeholder="Program">
    <option>-- Program --</option>
    <?php
    $query = mysql_query("select * from program order by program asc");

    while ($row = mysql_fetch_array($query)) {
        ?><option value="<?php echo $row['id_prog']; ?>"><?php echo $row['program']; ?></option><?php
    }
    ?>
</select>
Rotimi
  • 4,783
  • 4
  • 18
  • 27
  • First of all, you should think about using `mysqli` instead of the procedular `mysql` style. `mysql` is deprecated with PHP7 and will no longer be supported. Second, what is your problem with this code? – Alex Apr 04 '18 at 05:45
  • Thx for advice alex, sure ill use that after this one – Eko Asdarm Apr 04 '18 at 07:49

1 Answers1

0

As per your current requirement, the solution code would be like this:

<select class="form-control" id="id_prog" name="id_prog"  placeholder="Program">
    <option>-- Program --</option>
    <?php 
        $query=mysql_query("select * from program order by program asc");

        while($row=mysql_fetch_array($query)){
            $output = '<option value="' . $row['id_prog'] . '"';
            if($row['id_prog'] == YOUR_DESIRED_VALUE){
                $output .= ' selected="selected"';
            }
            $output .= '>' . $row['program'] . '</option>';
            echo $output;
        }
    ?>
</select>

Just change YOUR_DESIRED_VALUE with your desired value.

Reference: concatenation operator(.) in PHP


Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37