...

View Full Version : Help needed with PHP/Mysql dropdown list



coolguyraj
03-12-2009, 10:20 AM
I have this function that generate dropdown from mysql.

I want to midify this for update form (Ie i want to display the selected item first and the other choices after that.)

How can i do this.

Thanks.





function dropdown()
{
// open connection

$con = mysql_connect('test', 'user', 'pass');
// select database
mysql_select_db("test", $con);

//this code is bringing in the values for the dropdown.
$query="select buildingId,Name from buildings order by Name";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
while($row=mysql_fetch_array($result)){//Array or records stored
echo "<option value=$row[buildingId]>$row[Name]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
}
?>

kokjj87
03-12-2009, 10:53 AM
pass in a default value to this function..
example:
if you want the drop down with value1 selected
dropdown("value1");



<?php
function dropdown($default_value = null)
{

// open connection
$con = mysql_connect('test', 'user', 'pass');
// select database
mysql_select_db("test", $con);

//this code is bringing in the values for the dropdown.
$query="select buildingId,Name from buildings order by Name";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
while($row=mysql_fetch_array($result)){//Array or records stored
if($row[buildingId] == $default_value)
{
echo "<option value=$row[buildingId] selected='selected'>$row[Name]</option>";
}
else
{
echo "<option value=$row[buildingId]>$row[Name]</option>";
}

/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum