Function to make a select box
| From: | Leonard Burton | Date: | Tue, 04 Feb 2003 19:54:51 +0000 |
| Subject: | Function to make a select box | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-134323@lists.php.net to get a copy of this message | ||
Greetings,
In several scripts that I have written I have had to have a similar select
box. It will select from a table an auto number field and a name field
(like an id # and a customer name) and then it will display the name on the
screen and the variable posted will be the auto #.
Could anyone offer any suggestions? The following function prints a blank
select box.
function select_box($name, $table, $field, $value)
{
print "<select name=\"$name\">\n";
$result= "select auto, '$field' from '$table' order by '$field'
order by
name asc";
$result= mysql_query($result);
if ($result)
{
while ($row = mysql_fetch_array($result))
{
print "<option value=".$row["auto"];
if (($value != "no") && ($value==$row["auto"]))
{
print " selected";
}
print ">".$row[$field]."</option>\n";
}
mysql_free_result($result);
}
print "</select></td><td>\n";
}
Here is a working example that is not in a function:
//Get Existing Accounts and put them in a select box
print "<select name=account>\n";
$result= mysql_query("select * from ledger_accounts order by name asc");
if ($result)
{
while ($row = mysql_fetch_array($result))
{
print "<option value=".$row["auto"];
if ($account==$row["auto"])
{
print " selected";
}
print ">".$row["name"]."</option>\n";
}
mysql_free_result($result);
}
print "</select></td><td>\n";
Thanks,
Leonard Burton
lbburt0@pop.uky.edu