php - Display value for dropdown populated from database (SQL Server) -
i trying display saved value dynamic dropdown populated database. code below :
<?php $server = "xxx"; $options = array( "uid" => "xxx", "pwd" => "xxx", "database" => "xxx"); $conn = sqlsrv_connect($server, $options); if ($conn === false) die("<pre>".print_r(sqlsrv_errors(), true)); echo " "; $myquery="select department change_details id='2137'"; $fetched=sqlsrv_query($conn,$myquery) ; if( $fetched === false ) { die( print_r( sqlsrv_errors(), true ));} while($res=sqlsrv_fetch_array($fetched,sqlsrv_fetch_assoc)) { $department=$res['department']; } ?> <div class="container"> <!-- department --> <div class="form-inline clearfix"> <label class="col-md-5">department initiating request</label> <label name="department"></label> <div class="col-md-5"> <?php echo "<select name= 'department' class='form-control selectpicker' onchange='getstate(this.value)' required>"; echo '<option value="$department">'.'--please select department--'.'</option>'; $sql = "select id,name departments"; $query = sqlsrv_query($conn,$sql); $query_display = sqlsrv_query($conn,$sql); while($row=sqlsrv_fetch_array($query_display,sqlsrv_fetch_assoc)){ echo "<option value='". $row['name']."'>".$row['name']. '</option>'; } echo "</select>"; ?> </div> </div> </div><br/>
what working : dropdown being populated perfectly, , value saved database.
what need : want display saved value database , dropdown user edit field again. appreciate :)
you need current user selected value, when iterating on result set can this:
while($row=sqlsrv_fetch_array($query_display,sqlsrv_fetch_assoc)){ if ($department == $row['name']) { echo "<option selected='selected' value='". $row['name']."'>".$row['name']. '</option>'; continue; } echo "<option value='". $row['name']."'>".$row['name']. '</option>'; }
Comments
Post a Comment