86Stang Posted September 25, 2008 Share Posted September 25, 2008 Howdy do and a hidy ho neighbors, I've got this piece of code that pulls a list of project names from a table as a pull down but I'd like to have the default option be blank. Anyone care to school me? $project_qry = "SELECT * from tbl_projects order by project_name asc"; $project_result = mysql_query($project_qry) or die ("Error during query."); $project_drop = "<select name=\"task_project_id\">\n"; while ($row = mysql_fetch_array($project_result)) { if ($task_project_id == $row[project_id]) { $project_drop .= "<option value=\"$row[project_id]\" selected>$row[project_name]</option>\n"; } else { $project_drop .= "<option value=\"$row[project_id]\">$row[project_name]</option>\n"; } } $project_drop .= "</select>\n"; Mucho Grazious! Quote Link to comment https://forums.phpfreaks.com/topic/125846-how-to-add-a-blank-option-to-this-select-field/ Share on other sites More sharing options...
sKunKbad Posted September 25, 2008 Share Posted September 25, 2008 Replace: $project_drop = "<select name=\"task_project_id\">\n"; with $project_drop = "<select name=\"task_project_id\">\n<option value=\"\" selected=\"selected\">\n"; and replace $project_drop .= "<option value=\"$row[project_id]\" selected>$row[project_name]</option>\n"; with $project_drop .= "<option value=\"$row[project_id]\" >$row[project_name]</option>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/125846-how-to-add-a-blank-option-to-this-select-field/#findComment-650725 Share on other sites More sharing options...
JonnoTheDev Posted September 25, 2008 Share Posted September 25, 2008 Try this $project_qry = "SELECT * from tbl_projects order by project_name asc"; $project_result = mysql_query($project_qry) or die ("Error during query."); $project_drop = "<select name=\"task_project_id\">\n<option value=\"\"></option>\n"; while ($row = mysql_fetch_array($project_result)) { $project_drop .= "<option value=\"$row[project_id]\">$row[project_name]</option>\n"; } $project_drop .= "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/125846-how-to-add-a-blank-option-to-this-select-field/#findComment-650727 Share on other sites More sharing options...
kenrbnsn Posted September 25, 2008 Share Posted September 25, 2008 Just put one in before the while loop and take out the "selected" clause later: <?php $project_qry = "SELECT * from tbl_projects order by project_name asc"; $project_result = mysql_query($project_qry) or die ("Error during query."); $project_drop = "<select name=\"task_project_id\">\n"; $project_drop .= '<option value=" " selected> </option>' . "\n"; while ($row = mysql_fetch_array($project_result)) { $project_drop .= "<option value=\"$row[project_id]\">$row[project_name]</option>\n"; } $project_drop .= "</select>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/125846-how-to-add-a-blank-option-to-this-select-field/#findComment-650728 Share on other sites More sharing options...
86Stang Posted September 25, 2008 Author Share Posted September 25, 2008 Worked beautifully, Ken, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/125846-how-to-add-a-blank-option-to-this-select-field/#findComment-650826 Share on other sites More sharing options...
86Stang Posted September 26, 2008 Author Share Posted September 26, 2008 The code posted by Ken works beautifully but I have a followup question. I feel like a noob for not knowing this stuff but what can I say. Anyway I now need to let users edit this project so how can this code be modified to show the same pull down but with the option they selected at creation as the default? I've already got the UPDATE query in place and all that but I'm just having a problem getting this pull down to show properly. <?php $project_qry = "SELECT * from tbl_projects order by project_name asc"; $project_result = mysql_query($project_qry) or die ("Error during query."); $project_drop = "<select name=\"task_project_id\">\n"; $project_drop .= '<option value=" " selected> </option>' . "\n"; while ($row = mysql_fetch_array($project_result)) { $project_drop .= "<option value=\"$row[project_id]\">$row[project_name]</option>\n"; } $project_drop .= "</select>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/125846-how-to-add-a-blank-option-to-this-select-field/#findComment-651385 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.