Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ordered Dropdown list

Status
Not open for further replies.

bam720

Technical User
Sep 29, 2005
289
US
I wrote a sweet little php function to populate drop down lists.
Code:
	function PopulateSQLDropDown($Table,$Field,$SQL,$Sel){
		$Connection = mysql_connect("localhost","username","pass");
		mysql_select_db ("dbname");
		$sql = "SELECT * FROM `$Table` WHERE $SQL";
		$Query = mysql_query($sql);
		$i=0;
		while (mysql_fetch_row($Query)){
			$Result=mysql_result($Query,$i,$Field);
			if($Result!=$PrevResult)
				echo "<option value=\"".$Result."\"";
			if($Result == $Sel)
				echo "selected";
			echo ">".$Result."</option><br>";
			$i++;
			$PrevResult=$Result;
		}
		mysql_close($Connection);
	}
and it works 98% of what I want it to do. the only thing is that I feed it this Query for the $SQL varible.
$SQL = "`PrimeID` = '".$ClientName."' ORDER BY `EquipName` ASC";
What it creates is a drop down list that looks like this...
[Computer]
T1
Office
T2
All
--------

What I would like it to look like is:
[Computer]
Office
T1
T2
All
--------

Both the Computer Option and All option are declared outside of the function call like this...
Code:
	echo '<select name="Comp" onchange="this.form.submit()" selected='.$Comp.'>';
	echo "<option value=\"\">Computer</option><br>";
	$SQL = "`PrimeID` = '".$ClientName."' ORDER BY `EquipName` ASC";
	PopulateDropdown("Equipment","EquipName",$SQL,"");
	echo '<option value="All">All</option><br>';
	echo '</select>';

Anyone got any ideas? The ORDER BY command doesn't seem to have an effect... :(
 
feel free to use any part of that function if you like.
if($Result!=$PrevResult) just takes care of duplicate names from being populated as it is intended as a general use script and some client names are duplicated with different addresses.
 
Hi,
use: selected="selected", as that's XHTML compliant.

Also, I would do:
Code:
PopulateSQLDropDown($Table,$Field,$SQL[b] = 404[/b],$Sel)){
[b]if ($SQL != 404) {[/b]
... your code here...
        }
        mysql_close($Connection);
      [b]}[/b]
    }

I would also do some sort of better stringparsing than all the "gluing" of strings.. contentation or what it's called.

You might as well have $selectme = " selected=\"selected\"";
Eg, you can do:

if (current == selected ) {
$selectme = "selected=\"selected\"";
}
else {
$selectme = "";
}

which would lead to:
echo "<option value=\"{$Result}\" {$selectme}>{$Result}</option>";

Olav Alexander Mjelde
Admin & Webmaster
 
DaButcher I appreciate the effort but I think you may be confused by what I am asking. I want to drop down list to be ordered in a specific order namely in Alphabetical order if you notice this:
[Computer]
T1
Office
T2
All
--------

What I would like it to look like is:
[Computer]
Office
T1
T2
All
--------

Where Office is listed above T1. There may be more than T1, T2 for any given client which is why I am using php.

I do like your suggestions and that will make my code look cleaner so I am going to implement that. Any other ideas? Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top