Reputation: 15
I would like to prevent drop-down option duplicate in my edit page..
This is my query:
$res= mysql_query ("SELECT * FROM Acc_skills
INNER JOIN Accounts ON
Accounts.Role_ID = Acc_skills.FK_Role_ID
INNER JOIN Skills ON
Skills.Skills_id = Acc_skills.FK_Skills_ID
WHERE Acc_skills.Acc_skills_id = '$id'");
$row = mysql_fetch_array($res);
Here is my code:
<?php
echo '<option value= '.$row[3].' selected="selected">'.$row[3].' </option>';
echo '<option id="Low" value="Low">Low</option>
<option id="Average" value="Average">Average</option>
<option value="High">High</option>
</select>';
?>
I have managed to pull out the skill level data from my database (Eg, High), however, it shows duplicate drop down option now (Eg, there are 2 "High" options now). As the skill level option is manually typed in, how do i prevent having duplicate drop down options?
Thanks in advance!
Upvotes: 0
Views: 1382
Reputation: 16117
As per your comments, you need to use selected=""
option as per database value. you can use like that:
<select>
<option <?=($row[3] == 'Low' ? 'selected=""' : '')?> id="Low" value="Low">Low</option>
<option <?=($row[3] == 'Average' ? 'selected=""' : '')?> id="Average" value="Average">Average</option>
<option <?=($row[3] == 'High' ? 'selected=""' : '')?> value="High">High</option>
</select>
Explanation:
If $row[3]
is equal to "Average" or "High" or "Low" than select related option menu and add selected="" attribute in <option>
tag.
Side Note:
Please use mysqli_*
or PDO
extensions becuase mysql_*
extension is deprecated and not available in PHP 7
.
Upvotes: 2
Reputation:
first of all:
1- Please travel from mysql into mysqli or PDO , i suggest PDO.
2- if duplicated data in your database use "SELECT DISTINCT"
for fetching result.
3-if you have static option try like this:
<?php
echo "<select>";
echo '<option value= '.$row[3].' selected="selected">'.$row[3].' </option>';
echo '<option id="Low" value="Low">Low</option>
<option id="Average" value="Average">Average</option>
<option value="High">High</option>
</select>';
?>
or if you have fetching all rows act like this:
echo "<select>";
echo "<option id=Low value=Low >Low</option>
<option id=Average value=Average>Average</option>
<option value=High>High</option>";
foreach($result as $index){
echo " < optionvalue =$index[3] selected = 'selected' > $index[3]</option > ";
}
echo "</select>";
Upvotes: 1