Page 1 of 1

How to create a dynamic dropdown list

Posted: Sun Mar 25, 2007 6:00 am
by dexter
Hi Iam new to PHP,how to create a Dynamic drop down list using Mysql. Help me how to code for this.

Posted: Mon Mar 26, 2007 2:51 am
by leo
What do you want the dropdown list to do? Can you please be a little specific?

If you want to make a dropdown box using PHP + mysql, I suppose you can do something similar to:

[PHP]
<?php

// if you want to get data from the database..
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("Database_name", $db);

// query the database for something
$query = "SELECT * FROM table WHERE category = 'computers'";
$qHandler = mysql_query($query);

// list all the results from the database...
while ($data = myql_fetch_array($qHandler)) {
$dropdownlist .= "<option value=\"$data['value']\">$data['value']</option>";
}

// echo out the results from the database within a dropdown list...
echo "<select name=\"Dropdown_List\">" . $dropdownlist . "</select>";

?>
[/PHP]

I haven't tested the code yet... but that's probably what I would do if I need to make a dropdown list according to results from mysql using php.

Posted: Tue Mar 27, 2007 11:18 am
by Lixas
if you want dynamic list to be changed on site, you will have to use complicated web scripts in these days i think you probably can use AJAX, because this, what you want to do belongs to AJAX

Posted: Sun Apr 01, 2007 10:09 am
by delivi
I want it to be in a DHTML dropdown menu.

The database table contains a list of URLs and titles, they are to be dynamically retrieved and displayed in the menu.

Posted: Mon Apr 02, 2007 1:30 pm
by Lixas
Create table named "select_links" and it MUST have these fields:
link_target- varchar(255)
link_name- varchar (100)

[PHP]
<?
$query = 'SELECT * FROM select_links';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in select menu
echo "<form name=\"jump\">
<select name=\"menu\" onChange=\"location=****.jump.menu.options[****.jump.menu.selectedIndex].value;\" value=\"GO\">";

while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
echo "<option value=\"".$line["link_target"]."\">".$line["link_name"]."</option>\n";


echo "</select></form>";
?>[/PHP]

Posted: Mon Apr 02, 2007 6:23 pm
by sagheerparas
You can use pre-made scripts for your site including Drop Down List and many other scripts from http://www.dynamicdrive.com/

Posted: Tue Apr 03, 2007 5:08 am
by Lixas
correct me i f i'm wrong but in any way, any script from dynamicdrive will require connection to mysql and I think then you will place everything to the page, it will look more complicated than this one :P because this one is easiest (i think)