954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dynamic dropdowm list and mysql

how can i retrive data from database (mysql)and make it a drop down menu on a html form using php .and also if an iteam is selected from a fist dropdown before the next well become active.

dangutm
Newbie Poster
1 post since Mar 2007
Reputation Points: 10
Solved Threads: 0
 
how can i retrive data from database (mysql)and make it a drop down menu on a html form using php .and also if an iteam is selected from a fist dropdown before the next well become active.



:mrgreen: HI danguttm

I am in the process of doing the samething, kinda. I know how to tell you how to retrieve the data from the db, and I did an html tag with the droplist.

My db in mysql is movie ticket prices by ages. You have to make a connection to the db in mysql<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("name of the schema in tghe db") or die(mysql_error());


// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM movies")
or die(mysql_error());


// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );

// Print out the contents of the entry
$num=mysql_numrows($result);


hope this helps

wavyaquaeyes
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 
and also if an iteam is selected from a fist dropdown before the next well become active.


What do you mean by that?. Note if you're creating a dropdown list, you should put it in a form in order to work, or use some javascript to invoke some action upon change of the option.

Note that the dropdown list is a element with inside. Using the code from the previous post, you can create the dropdown using this php code:
[php]
$num = mysql_num_rows($connection);

echo '';
for ($i=0; $i<$num; $i++)
{
echo ''.$dataFromDb[].'';
}
echo '';
[/php]

Rhyan
Posting Whiz in Training
240 posts since Oct 2006
Reputation Points: 21
Solved Threads: 26
 

Another option. This will also have option to select a current option. This assumes that we have some $id set to signify which item will be selected in the list.
[php]
echo '';
while($row = mysql_fetch_array($rs)){
$selected = ($row['id'] == $id) ? 'selected' : '';
echo ''.$row['title'].'';
}
echo ''; [/php]
You will need to use Javascript for the doubleselect functionality you metioned. Or you could have this select refresh the page onChange="", and then render the second Select upon refresh. Javascript would be cleaner.

csscart
Newbie Poster
3 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You