drop down menu items

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matthew0786
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2009 8:01 am

drop down menu items

Post by matthew0786 »

Hi, i have a database for example

ID | Year | Name | Amount
---------------------------------
1 2007 test1 140
2 2008 test2 100
3 2008 test3 100
4 2009 test4 100
5 2009 test5 100
6 2009 test6 100
7 2009 test7 100

i want to be able to show the Years in my drop down box. My code is

Code: Select all

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="Users"; // Database name
 
// Connect to server and select databse.
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name", $con) or die("cannot select DB");
$result = mysql_query("SELECT * FROM statement);
$row = mysql_fetch_array($result);
 

Code: Select all

<select name="newYear">
        <option><?php echo $row["year"];?>"></option></br>
    </select>
At the moment that will display all the years in the database, but i dont want to show duplicate years. Is that possible??
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: drop down menu items

Post by McInfo »

Change your query to

Code: Select all

SELECT DISTINCT `year` FROM `statement` ORDER BY `year` DESC
Edit: This post was recovered from search engine cache.
Post Reply