I have a problem where the <option selected>
showing twice.
I am populating the <select><option>
with data from titles table
I want to show selected option based on data from taos table.
The data output is shown as: https://i.stack.imgur.com/P7V4E.png and https://i.stack.imgur.com/nBFPh.png
<?php
$title_sql = "SELECT * FROM titles";
echo "<select class='form-control' name='title_id'>";
if ($result = mysqli_query($conn, $title_sql)) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$dbselected = $tao['title_id'];
foreach ($row as $option) {
if ($dbselected == $option) {
?>
<option selected value='$row[id]'><?php echo $row['title']; ?></option>
<?php
} else {
}
}
echo "<option value='$row[id]'>$row[title]</option>";
}
mysqli_free_result($result);
}
echo "</select>"; ?>
<?php
$title_sql = "SELECT * FROM titles";
echo "<select class='form-control' name='title_id'>";
if ($result = mysqli_query($conn, $title_sql)) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$dbselected = $tao['title_id'];
foreach ($row as $option) {
if ($dbselected == $option) {
?>
<option selected value='$row[id]'>
<?php echo $row['title']; ?>
</option>
<?php
} else {
}
}
echo "<option value='$row[id]'>$row[title]</option>";
}
mysqli_free_result($result);
}
echo "</select>"; ?>
Any assistance would be appreciated.
CodePudding user response:
In the foreach
section you left else
section empty, and show your all select values outside the condition
try this
<?php
$title_sql = "SELECT * FROM titles";
echo "<select class='form-control' name='title_id'>";
if ($result = mysqli_query($conn, $title_sql)) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$dbselected = $tao['title_id'];
foreach ($row as $option) {
if ($dbselected == $option) {
?>
<option selected value='$row[id]'><?php echo $row['title']; ?></option>
<?php
} else {
echo "<option value='$row[id]'>$row[title]</option>";
}
}
}
mysqli_free_result($result);
}
echo "</select>"; ?>
CodePudding user response:
Your if
statement is outputting the option
with selected
when it is equal to the $dbselected
value. Then you are outputting the option (without selected
) unconditionally. Move the last echo
into the else {
portion of that if
to prevent it from being output if you have already outputted with selected
Also, you do not want the foreach ($row as $option) {
; as that is walking through the columns of the row, so the second echo
will be repeated for each column.
Also, be consistent. Either use echo
or drop out of PHP; don't do it one way and then the other.
Try it this way:
if ($result = mysqli_query($conn, $title_sql)) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$dbselected = $tao['title_id'];
## foreach ($row as $option) { ## TAKE THIS OUT
if ($dbselected == $option) {
?>
echo "<option selected value='{$row['id']}'>{$row['title']}</option>";
<?php
} else {
echo "<option value='{$row['id']}'>{$row['title']}</option>"; ## MOVED FROM BELOW
}
## } ## (End of foreach) TAKE THIS OUT
## echo "<option value='$row[id]'>$row[title]</option>"; ## MOVED THIS UP
}
I would actually use printf()
instead of the if-else
:
printf('<OPTION %s value="%s">%s</OPTION>',
($dbselected == $row['id'] ? "selected" : ""),
$row['id'],
htmlspecialchars($row['title']));
CodePudding user response:
I edited your question to show you how better formatting makes it easier to find your problem. your third echo statement is being executed first, It is outside your else block and should be inside it
<?php
$title_sql = "SELECT * FROM titles";
echo "<select class='form-control' name='title_id'>";
if ($result = mysqli_query($conn, $title_sql)) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$dbselected = $tao['title_id'];
foreach ($row as $option) {
if ($dbselected == $option) {
?>
<option selected value='$row[id]'>
<?php echo $row['title']; ?>
</option>
<?php
} else {
}
}
echo "<option value='$row[id]'>$row[title]</option>";
}
mysqli_free_result($result);
}
echo "</select>"; ?>
CodePudding user response:
Check This Code
$sql = "SELECT * FROM titles";
$query = mysqli_query($con,$sql);
echo "<select class='form-control' name='title_id'>";
while($row = mysqli_fetch_assoc($query)) {
$dbselected = $tao['title_id'];
if ($dbselected == $row["title_id"]) {
echo "<option selected value='".$row[id]."'>".$row['title']."</option>";
} else {
echo "<option value='".$row[id]."'>".$row['title']."</option>";
}
echo "</select>";
?>