...

View Full Version : Resolved Option values from database



greens85
10-21-2009, 04:48 PM
Hi all,

I'm currently retrieving some values from a database and placing them into a drop down box with the following code:


<select name="lea">
<option value="">-- Search All --</option>
<?php
$query = mysql_query("SELECT Leaname FROM schools ORDER BY Leaname ASC");
while ($result = mysql_fetch_array($query)) {
?>
<option value="<?php echo $result['Leaname']?>"><?php echo $result['Leaname']?></option>
<?php
}
?>
</select>

The problem is, because there is more than one instance of each value, the option box is returning something like this:

Value 1
Value 1
Value 1
Value 1
Value 1
Value 1
Value 2
Value 2
Value 2
Value 2
Value 3
Value 3
Value 3

Where as I only want it to show one instance of each, i.e.

Value 1
Value 2
Value 3

Is there a php workaround for this?

I know I could manually code the values into the html, but there are some many it would take a long time!

Thanks,

Greens85

oesxyl
10-21-2009, 04:51 PM
mysql, not php:


select distinct Leaname FROM schools ORDER BY Leaname ASC

in php you can use array_uniq but is a complication. in my opinion.

best regards

greens85
10-21-2009, 04:54 PM
mysql, not php:


select distinct Leaname FROM schools ORDER BY Leaname ASC

in php you can use array_uniq but is a complication. in my opinion.

best regards

Thats done the trick, many thanks for your help, its much appreciated! :thumbsup:

oesxyl
10-21-2009, 05:02 PM
Thats done the trick, many thanks for your help, its much appreciated! :thumbsup:
you are welcome. A suggestion, each time you mix sql with any server side script get all you can from sql in the shorter you can. This will make your life easy.
I say this because I read in another post of yours that you are not so good with sql, try to improve because you will need it, :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum