Jump to content

How to add a blank option to this select field


86Stang

Recommended Posts

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!

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";

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";

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

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";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.