Jump to content

[SOLVED] Help with drop down box


segyn

Recommended Posts

My drop down menu works fine but what I want is one of the options in the database for Status is Good.

 

I want that to be the default option. If i enter selected in the option value such as selected="good" it just defaults to putting obsolete as the default value.

 

Any Ideas on how to get this to work will be greatly appreciated.

 

I am currently running php ver. 4.2.3

 

The site i'm working on is just to keep track of all the tools we have at the company I work for. So i have a lot of drop down boxes i made like this so i could just include them into each page.

 

 

 

<?php

	echo"<tr bgcolor='Black'>
		<td bgcolor='silver'> Status:
		<td bgcolor='silver'>";

include("conn.php");

		$sql = mysql_query("SELECT * FROM Tools.Status");
		echo "<select name ='Status'>";
		while($row = mysql_fetch_array($sql))
{

		echo "<option>" . $row['G_Status'] ."</option>";

}
		echo "</select>";
		echo "</tr>";
		mysql_close($con);

?>

Link to comment
https://forums.phpfreaks.com/topic/53728-solved-help-with-drop-down-box/
Share on other sites

i'm not sure how you know when something is going to be the default so i guessed its when

$row['G_Status'] == "Good", so change that if needed

i think this is what you want!

 

$default = ($row['G_Status'] == "good")?" selected ":"";
echo "<option $default>{$row['G_Status']}</option>";

 

EDIT:

replacing the

echo "<option>" . $row['G_Status'] ."</option>";

line

Try changing this:

 

while($row = mysql_fetch_array($sql))
{

echo "<option>" . $row['G_Status'] ."</option>";

}

 

To:

 

while($row = mysql_fetch_array($sql))
{
if($row['G_Status'] == "good") {
echo "<option value="" selected>" . $row['G_Status'] ."</option>";
}
else {
echo "<option value="">" . $row['G_Status'] ."</option>";
}
}

 

Chris

Thanks this  made it work.

 

Try changing this:

 

while($row = mysql_fetch_array($sql))
{

echo "<option>" . $row['G_Status'] ."</option>";

}

 

To:

 

while($row = mysql_fetch_array($sql))
{
if($row['G_Status'] == "good") {
echo "<option value="" selected>" . $row['G_Status'] ."</option>";
}
else {
echo "<option value="">" . $row['G_Status'] ."</option>";
}
}

 

Chris

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.