View Full Version : Array for a Drop Down Menu???
RobertMaltby 05-04-2005, 10:35 PM Hello Everyone, Thanks for Comming in to Help...
Im trying to create a Drop Down Menu that will be however long the number of people is in my MYSQL DB..
Say i have 30 people in the table i would need the drop down show all 30... Same way if i have 5 people or 50 peopl (i dont plan on going over 100 but it would be nice if i could)
Thanks for all suggestions and Help :)
TehBooster 05-04-2005, 11:22 PM <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
$sql = "SELECT name FROM myTable
ORDER BY name ASC"; // or use your query
if(! $result = mysql_query($sql) )
{
die( mysql_error() . '<br />SQL Query: <code>' . $sql . '</code>');
}
$option_array = array();
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) )
{
$option_array[] = '<option value="' . $row['name'] . '">' . $row['name'] . '</option>';
}
echo '<select name="people">';
echo implode( '', $option_array );
echo '</select>';
?>
</form>
Looking for something like that?
[edit #1] forgot a few things :D
RobertMaltby 05-04-2005, 11:30 PM Thats Perfect...
Just so i can make sure this is right... (i havent done much of arrays and forms and drop downs together...)
Already connected to mysql db...
$query_dropdown = "SELECT email FROM users
ORDER BY email ASC"; // or use your query
if(! $result = mysql_query($query_dropdown) )
{
die( mysql_error() . '<br />SQL Query: <code>' . $sql . '</code>');
}
$option_array = array();
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) )
{
$option_array[] = '<option value="' . $row['name'] . '">' . $row['name'] . '</option>';
}
echo implode( '', $option_array );
<option value="' . $row['email'] . '">' . $row['email'] . '</option>
That sets the Value of each to the EMAIL name and then displays the Email... Simple enough.. lol..
RobertMaltby 05-04-2005, 11:30 PM Thats Perfect...
Just so i can make sure this is right... (i havent done much of arrays and forms and drop downs together...)
Already connected to mysql db...
$query_dropdown = "SELECT email FROM users
ORDER BY email ASC"; // or use your query
if(! $result = mysql_query($query_dropdown) )
{
die( mysql_error() . '<br />SQL Query: <code>' . $sql . '</code>');
}
$option_array = array();
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) )
{
$option_array[] = '<option value="' . $row['email'] . '">' . $row['email'] . '</option>';
}
echo implode( '', $option_array );
<option value="' . $row['email'] . '">' . $row['email'] . '</option>
That sets the Value of each to the EMAIL name and then displays the Email... Simple enough.. lol..
**Sorry for the double post**
TehBooster 05-04-2005, 11:33 PM That should do :) If you have problems just PM me or post here :)
RobertMaltby 05-04-2005, 11:49 PM I have used "implode" before... just curious if this would work...
$db = new Database;
$db->Connect($CONF['dbname']);
$query_dropdown = "SELECT `$CONF[table_prefix]email` FROM `$CONF[table_prefix]users`
ORDER BY email ASC"; // or use your query
if(! $result = mysql_query($query_dropdown) )
{
die( mysql_error() . '<br />SQL Query: <code>' . $sql . '</code>');
}
$option_array = array();
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) )
{
$option_array[] = '<option value="' . $row['email'] . '">' . $row['email'] . '</option>';
}
$result = $db->Query($query);
echo implode( '', $option_array );
I have the Form in the output(""); lines... so i could just set a VAR (like $dropdown)
$dropdown = implode('',$option_array); ?
Thanks
TehBooster 05-04-2005, 11:52 PM implode(); (http://www.php.net/implode) only returns a string, so you can do $dropdown = implode('', $option_array); to output it elsewhere.
RobertMaltby 05-05-2005, 12:54 AM ok..
I tried that and it wouldnt work...
So i changed somethings and did this
$db->Connect($CONF['dbname']);
$query_dropdown = "SELECT `$CONF[table_prefix]email` FROM `$CONF[table_prefix]users` ORDER BY email ASC"; // or use your query
$dropdown = $db->Query($query_dropdown);
if($dropdown)
{
$option_array = array();
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) )
{
$option_array[] = '<option value="' . $row['email'] . '">' . $row['email'] . '</option>';
}
echo implode(',', $option_array );
}
else
{
die( mysql_error() . '<br />SQL Query: <code>' . $sql . '</code>');
}
and then later on in the page i called it...
output("<tr bgcolor=$_TEMPLATE[light_background]>
<td width=50% style=\"border-left: 1px solid $_TEMPLATE[border_color];border-bottom: 1px solid $_TEMPLATE[border_color]\">
<b>Login Name:</b> (*)<br> This is for both FTP and Control Panel.
</td>
<td width=50% style=\"border-right: 1px solid $_TEMPLATE[border_color];border-bottom: 1px solid $_TEMPLATE[border_color]\">");
$dropdown;
output("</td></tr>");
Any help?
TehBooster 05-05-2005, 12:58 AM you still need to use echo or your output(); function
echo $dropdown; or output($dropdown);
RobertMaltby 05-05-2005, 01:32 AM I did that... then found a Mysql error on my Users.php page...
http://profound-hosting.com/panel/admin/game-install.php
Thats the page im trying to get it on... but its just showing "Resource id #7" as an error???
RobertMaltby 05-05-2005, 01:57 AM OMFG FINALLY!!!! lol...
Thanks So much :)
When i did echo it thru it out of my page outline... but when i did " output(" $dropdown "); it worked :)
Thanks Again
VolkNet 05-05-2005, 12:10 PM I have a code does this exactly.
If you're still having trouble with it, feel free to PM me.
RobertMaltby 05-05-2005, 12:18 PM I figured it out last night with teh booster's help...
Thanks though.
|