sherinks sherinks - 1 year ago 62
SQL Question

select the name as selected in dropdown

Query for select God from God table

$sth =$dbh->prepare("SELECT god_id,god_name_ml,god_name_en,image,info_ml,info_en,details_ml,
details_en,rounds_ml,rounds_en,mantra_ml,mantra_en,display_order FROM god");

Query for select deity from deity table.Here god_id is the foreign key from god table

$stmt = $dbh->prepare("SELECT deity_id,god_id,deity_name_ml,deity_name_en,info_ml,info_en,details_ml,
details_en,mantra_ml,mantra_en,display_order FROM deity
WHERE deity_id = :deity_id");
$result = $stmt->fetchAll();

display code for displaying god in dropdown from god table

<?php while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { ?>
<option value="<?php echo $row['god_id'];?>">
<?php echo $row['god_name_en']; ?>
<?php } ?>

what I need is, in dropdown list the given god_id in god table is present in deity table make it as selected.
only I need is selected Id's are selected

Answer Source

Just check both the id's are same or not, try this :

<?php while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { ?>
  <option <?php if($row['god_id'] == $god_id) { echo "selected='selected'"; } ?> value="<?php echo $row['god_id'];?>"><?php echo $row['god_name_en']; ?></option>
<?php } ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download