Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.