Jump to content

[SOLVED] Drop Down Menu Default Selection - Extra item shown


Recommended Posts

Hello,

 

I have an internal website that uses drop down menus to make categories for storing information. When you add a new item to the database you pick a category from the drop down list. Now when you edit the page I want the same drop down list created but I want to automatically select whichever category the user selected when they added the item to the database.

 

Here is the function that I wrote to do this:

/*
Function_Categories_List()
*/
function Function_Categories_List() {
//	Import the issue_category variable.
global $issue_category;

//	Create the SQL statement
$sql = "SELECT * FROM categories";

//	Execute the SQL statement
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
	extract($row);
	if ($category_text == $issue_category) {
		echo("<option value=\"".$category_text."\" selected>$category_text</option>");
	}
	echo("<option value=\"".$category_text."\">$category_text</option>");
}
}

 

It works however it shows an additional item in the dropdown box. Which category shows up as "selected" a second one is also displayed. I can understand /why/ it is displayed, but I cannot figure out how to delete that extra item.

 

The end goal is for the program to read the assigned category for this item from the database. It then grabs all the available categories from the database. It presents this as a dropdown box and automatically selects the correct category.

 

Thank you so much for your help!

 

Sincerely,

 

-Richard

 

 

Woops - I spoke too soon.

 

That did remove the extra item but now all of the items in the drop down have the "selected" code attached to them, which means that the correct item is not selected by default.

 

-Richard

 

I apologize I did not get a chance to test the latest piece that you posted. I figured out a different way of displaying the data and having it work.

 

Here is the function that I created:

/*
Function_Categories_List()
*/
function Function_Categories_List() {
//	Import the issue_category variable.
global $issue_category;

//	Create the SQL statement
$sql = "SELECT * FROM categories";

//	Execute the SQL statement
$result = mysql_query($sql);

//	How many rows are we looking at?
$num_rows = mysql_num_rows($result);

//	For each row we need to get the data and check to see how to display it. Then display it!
for ($i = 1; $i <= $num_rows; $i++) {
	$row = mysql_fetch_array($result);
	extract($row);
	if ($issue_category == $category_text) {
		echo("<option value=\"".$category_text."\" selected>$category_text</option>");
	}
	else {
		echo("<option value=\"".$category_text."\">$category_text</option>");
	}
}
}

 

Thank you for your help!

 

 

 

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.