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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

drop down menus from mysql?

Status
Not open for further replies.

ndog4ever

MIS
Feb 6, 2002
92
US
what is easiest way to write code to display a drop down menu that reads from a mysql database. I have a list of routers and their interface cards. I want the user to be able to select the router and or interface, and click a submit button that passes that variable somewhere so i can use it in an array. Im kinda new to php, so any help would greatly be appreciated.

thanks

 
This sort of quick and dirty, but it should give a good idea:
function selRoutersAndCards()
{
$sql = "SELECT router, card FROM table";

$rs = mysql_query($sql, )or die("Connection to DataBase failed");
for($i = 0; $i < mysql_num_rows( $rs ); $i++)
{
$tmp = mysql_fetch_row( $rs );
print(&quot;<OPTION value=\&quot;$tmp[0]:$tmp[1]\&quot;>$tmp[1]</OPTION>\n&quot;);
}
}

Call like this:
<SELECT name=&quot;selRouter&quot; size=&quot;1&quot;>
<?php
selRoutersAndCards(); ?>
</SELECT>

Not pretty but effective
 
hey, i think i am following your logic ok, but im not to sure about the select statement at the bottom. Its giving me an error. Also, when i choose whatever router, or interface card, can i pass that as a variable somewhere else later on? I guess what the print function is gonna do is print all the columns and rows for that particular interface. well sorry for all the questions as I am just learning this language.

thanks for the help so far.

<?

$db_name = &quot;inventory&quot;;
$table_name = &quot;routers&quot;;
$connection = @mysql_connect(&quot;localhost&quot;, &quot;nate&quot;, &quot;nate&quot;) or die(&quot;Couldn't connect.&quot;);

$db = @mysql_select_db($db_name, $connection) or die(&quot;Couldn't select database.&quot;);

$result = @mysql_query($sql, $connection) or die(&quot;Couldn't execute query.&quot;);

function selRoutersAndCards()
{
$sql = &quot;SELECT * from router&quot;;

$rs = mysql_query($sql, $connection)or die(&quot;Connection to DataBase failed&quot;);
for($i = 0; $i < mysql_num_rows( $rs ); $i++)
{
$tmp = mysql_fetch_row( $rs );
print(&quot;<OPTION value=\&quot;$tmp[0]:$tmp[1]\&quot;>$tmp[1]</OPTION>\n&quot;);
}
}
<SELECT name=&quot;selRouter&quot; size=&quot;1&quot;>
<?php
selRoutersAndCards(); ?>
</SELECT>

?>
 
The <SELECT> at he botom is HTML, so you need to jump out of php there and jump back in if you need to:

?>//jump out of php by closing tag here!

<SELECT name=&quot;selRouter&quot; size=&quot;1&quot;>
<?php
selRoutersAndCards(); ?>
</SELECT>

<?php//jump back in here <---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top