FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


"Drop Down List Box" Using PHP and MySql





rjraaz
Here we are going to create a “Drop Down List Box” by using php and mysql.

Ok lets start!


1. Create Database example

for creating database you can use syntax : create database example;

in phpmyadmin or mysql interface

2. Create Tbale Name

for creating table in database example you can use syntax :

use example;

create table names
( ID integer auto_increment,
name varchar(30),
primary key (yearID)
);

insert into nemes (ID, name) values (’0′, ‘name1′);
insert into nemes (ID, name) values (’0′, ‘name2′);
insert into nemes (ID, name) values (’0′, ‘name3′);
insert into nemes (ID, name) values (’0′, ‘name4′);
insert into nemes (ID, name) values (’0′, ‘name5′);
insert into nemes (yearID, year) values (’0′, ’2012-2013′);

3. PHP Code

The following PHP script (list.php), which would form part of a form, will extract all the entries from the ‘names’ database table, and use them to populate the drop down list box.

List.php

<?php

$data = @mysql_query(“select * from names”);
echo “<p>Select a Name: n”;
echo “<Select Name=”ID”>n”;
while ($row = mysql_fetch_assoc($data))
{
$ID = $row['ID'];
$year = $row['name'];
echo “<option value=$ID>$namen”;
}
echo “</select>n”;
echo “</p>n”;
?>

i hope this post helpfull
rickylau
I doubt if this works:

What is the structure of the database?
Code:
primary key (yearID)
Code:
insert into nemes (yearID, year) values (’0′, ’2012-2013′);
Where is yearID and year?

Code:
$data = @mysql_query(“select * from names”);
This line fails because no database connections have been made
Code:
echo “<option value=$ID>$namen”;
I think the option tag should be close before going on the loop, and you don't have variable namen at any moment.


I can get a little about what your example is going to do, but it seems to have a lot of mistakes and I think these make other confuse and go "why this example just don't work? Is it my fault?".
ChrisSunith
Dear Mr.RJRAAZ,

Thank you very much!! It has indeed helped me with my project. God bless you richly.

--
ChrisSunith
sonam
Is this little bit too much building database just for one drop down menu. For some simple things like drop down menu, I am prefer to use template and some items (variables) what I can easy change and include with custom.php without using mysql.

But your script is perfect if you have some saved database with many different entries and you want build drop down menu pulling out some information's from this it. In that case you don't need to build new data base.

Sonam
Related topics
Windows Tips&tricks!
Drop down menu things
[JS] selecting an option from a drop down box
What is useful way in this php+mysql problem?
Allow IPB or Vbulletin
A very good PHP MySQL Tutorial
array check box on php script using $_POST
IP => Country => Drop Down Box!
Top down list and MySQL
Help me about Drop Down List Boxe?
how to put what you have query in a drop down list?
Menu + drop down
Do you lose PR when using a drop down menu?
Help displaying pdfs using PHP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.