Page 1 of 1

Dynamic drop down - How to Keep from duplicating entry?

Posted: Mon Mar 12, 2007 9:29 am
by kentopolis
Hi there, this forum has been tremendously helpful for me...
I need to know how to keep a dynamicly generated (from mysql) drop down from repeating entries, when entries are repeated in the table. Here is my current code which is working fine:

Code: Select all

<?
$query="SELECT ptype FROM dogs";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

$result = mysql_query ($query);
echo "<select name=q></option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[ptype]>$nt[ptype]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box 
?>
http://www.lovealotkennels.com/test2.php is where I am testing this, the drop down works and it searches properly, I just can't seem to figure out how to get it to not display "Yorkshire Terrier" a hundred times. Thanks for any suggestions!

Posted: Mon Mar 12, 2007 9:37 am
by blackbeard
Try

Code: Select all

$query="SELECT DISTINCT ptype FROM dogs";

Posted: Mon Mar 12, 2007 11:14 am
by aaronhall
FYI, always quote array keys, e.g. $nt['ptype']

Posted: Mon Mar 12, 2007 11:55 am
by kentopolis
distinct worked perfectly!!! Thanks!!!

Posted: Mon Mar 12, 2007 12:58 pm
by kentopolis
is there a way to sort the resulting options so they are alphabeticall?

I tried "sort($nt)"

didn't work

I tried "order by ptype" which reordered it but not alphabetically? What am I doing wrong...

Posted: Mon Mar 12, 2007 1:03 pm
by kentopolis
I am so dumb, it was order by, the problem was the lady who had typed in the information put in a " " space so it was ordering that first, I need to strim out that space I guess...