Sunday, October 18, 2015

Snippet - PHP select option from database differ with default

<select class="form-control" name="lokasi" required="true">
 <? /* Get current value in selected row as first option value */ ?>
 <option value="<?php echo $row['lokasi']; ?>"><?php echo $row['lokasi']; ?></option>

 <? /* Gelt list of option in another table */ ?>
 <?php
 $BlokBilik = $row['lokasi'];
 $sql = "SELECT * FROM lokasi";
 $result1 = mysql_query($sql) or die("Error : " . mysql_error());
 while ($rowLokasi = mysql_fetch_array($result1)) {
     $lokasi = $rowLokasi['blok'] . " / " . $rowLokasi['bilik'];
     /* Compare string are same or not */
     if (strcmp($lokasi, $BlokBilik) != 0) {
         echo "<option value='" . $lokasi . "'>" . $lokasi . "</option>";
     }
 }
 ?>
</select>

No comments:

Post a Comment