Jump to content

Basic Drop Down Menu!!


dzwestwindsor

Recommended Posts

Hello!

 

This is my first post here.

 

I'm usually do programming in Ruby (Rails) but for a special occasion I need PHP.

 

So hi PHP people! I come in peace  ;)

 

My question is quite simple. How do you make a drop down menu? Most preferably with database populated fields in it.

 

In the Rails community, this really helped me make these menus in Rails: http://www.kahfei.com/?p=23

 

Could someone briefly write something like that too? (The place holders)

 

Orrr you guys can just explain how to do it. The ones I found on the internet were quite confusing and not explained enough.

 

Help anyone? Thank you :)

 

 

Link to comment
Share on other sites

Something like this should work for you...

 

$sql 'SQL STATEMENT HERE TO GET MENU OPTIONS';
$result = mysql_query($sql, $db_connection) or die(mysql_error()); // Can omit the db_connection if you currently have one open
// start select
echo '<select name="whatever">';
while ($row = mysql_fetch_array($result)) {
	// inputs the value of $row['index_of_info_wanted'] into the option
echo '<option value="'.$row['index_of_info_wanted'].'>'.$row['index_of_info_wanted'].'</option>"';
}
// end select
echo '</select>';

 

Just replace the sql and $row[] calls. Hope that gets you started

 

 

**EDIT forgot the error handling

Link to comment
Share on other sites

like this

<?php
//use php loop to print all county name in selectbox

if(! $conn=mysql_connect("localhost", "root", "demo"))
    {
        die("Could't connect to database server..");
    }
    mysql_select_db("online_application",$conn) or die(mysql_error());

echo "<select name='country'>";
$result = mysql_query("SELECT * FROM tbl_country");
while($row = mysql_fetch_assoc($result))
{
   echo "<option value='{$row['id']}'>{$row['country_name']}</option>";
}

echo "</select>";
?>

Link to comment
Share on other sites

GREAT! It worked like a charm. Thank you, both of you!!

 

But one more thing- how would I save the option that the user chose as an instance variable?

 

I'm guessing I would need the form tag, but what are the params, and how can i assign the value selected as an instance variable?

 

Thank you for all the help so far!!!

 

(realizing that PHP wasn't as bad as I thought it was... in fact, it's pretty good :D)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.