944,147 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 8785
  • PHP RSS
Mar 1st, 2007
0

dynamic dropdowm list and mysql

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dangutm is offline Offline
1 posts
since Mar 2007
Mar 1st, 2007
0

Re: dynamic dropdowm list and mysql

Click to Expand / Collapse  Quote originally posted by dangutm ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wavyaquaeyes is offline Offline
20 posts
since Feb 2007
Mar 2nd, 2007
0

Re: dynamic dropdowm list and mysql

Click to Expand / Collapse  Quote originally posted by dangutm ...
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 <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]
Reputation Points: 21
Solved Threads: 26
Posting Whiz in Training
Rhyan is offline Offline
240 posts
since Oct 2006
Mar 3rd, 2007
0

Re: dynamic dropdowm list and mysql

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.
Last edited by csscart; Mar 3rd, 2007 at 4:43 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
csscart is offline Offline
3 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: postnuke installation
Next Thread in PHP Forum Timeline: request help :listing directories with php >> changing to use shell commands: how to?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC