Page 1 of 1

drop down menu items

Posted: Mon Apr 20, 2009 9:28 pm
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??

Re: drop down menu items

Posted: Mon Apr 20, 2009 9:50 pm
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.