How to create a dynamic dropdown list

Any problem with PHP can be disscused here
Post Reply
dexter
Posts: 32
Joined: Fri Mar 09, 2007 1:18 pm

How to create a dynamic dropdown list

Post by dexter »

Hi Iam new to PHP,how to create a Dynamic drop down list using Mysql. Help me how to code for this.


leo
Posts: 13
Joined: Tue Mar 13, 2007 4:56 am

Post 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.
Lixas
Posts: 750
Joined: Wed Feb 16, 2005 4:21 pm

Post 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
Image
delivi
Posts: 454
Joined: Sun Mar 26, 2006 1:24 pm
Contact:

Post 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.
Lixas
Posts: 750
Joined: Wed Feb 16, 2005 4:21 pm

Post 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]
Image
sagheerparas
Posts: 12
Joined: Sat Feb 17, 2007 11:34 pm

Post by sagheerparas »

You can use pre-made scripts for your site including Drop Down List and many other scripts from http://www.dynamicdrive.com/
Lixas
Posts: 750
Joined: Wed Feb 16, 2005 4:21 pm

Post 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)
Image
Post Reply