Reputation: 387
I have a drop down select
populated dynamically from a MySQL database
and I need to default the drop down value to display based on which record the user selected. What I have so far is not displaying any values in the drop down. But I have code that is outside of the select option statement and it displays the defaulted value in bold. I just can't seem to get the drop down to walk through the if statement logic.
A user selects a record from the data table and clicks edit. For example the user selected a row that is highlighted in grey then clicks edit that sends them to an edit form
Edit form: I pass the unit id to the edit form and pre-populate the input fields based on the passed id but here is where I can't populate and set the default drop down option values
Here is the code behind the edit form for the drop down select (div id drop down)
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','*********');
define('DBNAME','fdmamaint');
if (!$db = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME)) die("Can't connect to database");
// Retrieve the record the user selected
$id = htmlspecialchars($_GET["id"]);
// Query the depunits table based on the user selection
$sql = "SELECT unit_id, div_id, title_org, short_desc, long_desc, unit_desc, avail_ind "
. "FROM depunits "
. "WHERE unit_id=$id";
if (!$result = $db->query($sql)){
die("There was an error running the query [" .$db->error. "]");
}
// Walk through the result set and assign variables for reference later on in the script
while ($row = $result->fetch_assoc()){
$unitId = $row['unit_id'];
$divId = $row['div_id'];
$titleOrg = $row['title_org'];
$shortDesc = $row['short_desc'];
$unitDesc = $row['unit_desc'];
$longDesc = $row['long_desc'];
$enabled = $row['avail_ind'];
}
// Div Id - query database to get drop down select options
$divSelect = "SELECT div_id, long_desc FROM depdivisions "
. " WHERE div_id <> '' and avail_ind='Y' and active_ind='Y'"
. " ORDER BY div_id";
if (!$divResult = $db->query($divSelect)){
die("There was an error running the query [" .$db->error. "]");
}
.......
Dynamic drop down select:
<!-- Div Id -->
<label for="divId" class="control-label">Div Id</label>
<select class="form-control" name="divId" id="divId">
<option value=" "></option>
<?php
while ($rowDivs = $divResult->fetch_assoc())
{
$data = '37' .$rowDivs['div_id'];
// if the value of $data matches the value of div id of the
// user selected row in the database then give that option
// value the selected tag
if ( $data == $divId)
{
echo "<option value=". $data ."selected>";
echo $data ;
echo "</option>";
}
else
{
echo "<option value=". $data .">";
echo $data;
echo "</option>";
}
}
Can I have an if/else statement within the php while statement? Is that maybe why I'm not getting any data results in the drop down?
Upvotes: 0
Views: 2908
Reputation: 387
I figured it out:
I needed to use an inline if statement like so:
<!-- Div Id -->
<label for="divId" class="control-label">Div Id</label>
<select class="form-control" name="divId" id="divId">
<option value=" "></option>
<?php
while ($rowDivs = $divResult->fetch_assoc()) {
$data = '37' .$rowDivs['div_id'];
echo "<option value=$data" .($data == $divId ? ' selected="selected"' : '') . ">$data</option>";
}
?>
</select>
Upvotes: 1