| | |
dynamic dropdowm list and mysql
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
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.
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
•
•
•
•
and also if an iteam is selected from a fist dropdown before the next well become active.
Note that the dropdown list is a <select></select> element with <option></option> inside. Using the code from the previous post, you can create the dropdown using this php code:
[php]
$num = mysql_num_rows($connection);
echo '<select id="mylist" name="mylist">';
for ($i=0; $i<$num; $i++)
{
echo '<option value=".$dataFromDb[].">'.$dataFromDb[].'</option>';
}
echo '</select>';
[/php]
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
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 '<select id="mylist" name="mylist">';
while($row = mysql_fetch_array($rs)){
$selected = ($row['id'] == $id) ? 'selected' : '';
echo '<option value=".$row['id']." '.$selected.'>'.$row['title'].'</option>';
}
echo '</select>'; [/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.
[php]
echo '<select id="mylist" name="mylist">';
while($row = mysql_fetch_array($rs)){
$selected = ($row['id'] == $id) ? 'selected' : '';
echo '<option value=".$row['id']." '.$selected.'>'.$row['title'].'</option>';
}
echo '</select>'; [/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.
Last edited by csscart; Mar 3rd, 2007 at 4:43 pm.
![]() |
Similar Threads
- List View save data from it to mysql (VB.NET)
- Dynamic dropdown list (ASP.NET)
- Dynamic Next/Prev Buttons in PHP/MySQL (MySQL)
- Help w/ dynamic list funtction definitions (C++)
- Feeding a value in a dropdown list to another dropdown list (PHP)
Other Threads in the PHP Forum
- Previous Thread: postnuke installation
- Next Thread: request help :listing directories with php >> changing to use shell commands: how to?
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail match menu mlm mod_rewrite multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion regex remote script search server sessions sms smtp soap source space sql strip_tags survey syntax system table tutorial undefined update upload url validation validator variable video virus web window.onbeforeunload=closeme; xml youtube





