Reputation: 37504
This may be complicated to explain. I have set up a timesheet system where employees can insert into a mysql db their own timesheet records. this works fine but i've given them the option to edit an item they have inserted, so what i have is a dropdown displaying from mysql all project numbers available for them to pick but i also want to set the default value of the dropdown to the project number which they selected when they inserted the record (ie. to avoid having to remember what project it was)
here is my code:
$result1 = mysql_query("select project_no from `projects`") or die(mysql_error());
echo '<select name="project" class="project">';
while($row1 = mysql_fetch_array($result1)){
echo "<option selected='yes' value=".$row1['project_no'].">".$row1['project_no']."</option>";
}
echo '</select>';
The record is stored in a table called timesheets, so i can do a SELECT from that but i'm not sure how to set the right project number as the default value in the above dropdown?
Does that make sense?
Upvotes: 0
Views: 1909
Reputation: 255105
$project_to_select = 42;
while($row1 = mysql_fetch_array($result1)){
echo "<option " . ($row1['project_no'] == $project_to_select ? "selected='selected'" : "") . " value=".$row1['project_no'].">".$row1['project_no']."</option>";
}
Upvotes: 1
Reputation: 1664
Store the project number in a variable, e.g., $projnum, and compare it with each value.
$result1 = mysql_query("select project_no from `projects`") or die(mysql_error());
echo '<select name="project" class="project">';
while($row1 = mysql_fetch_array($result1)){
echo "<option ";
if ($row1['project_no'] == $projnum) { echo 'selected="selected"; }
echo " value=".$row1['project_no'].">".$row1['project_no']."</option>";
}
echo '</select>';
Upvotes: 1