Dynamic drop down - How to Keep from duplicating entry?

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
kentopolis
Forum Newbie
Posts: 11
Joined: Mon Mar 05, 2007 12:16 pm

Dynamic drop down - How to Keep from duplicating entry?

Post 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!
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Try

Code: Select all

$query="SELECT DISTINCT ptype FROM dogs";
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

FYI, always quote array keys, e.g. $nt['ptype']
kentopolis
Forum Newbie
Posts: 11
Joined: Mon Mar 05, 2007 12:16 pm

Post by kentopolis »

distinct worked perfectly!!! Thanks!!!
kentopolis
Forum Newbie
Posts: 11
Joined: Mon Mar 05, 2007 12:16 pm

Post 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...
kentopolis
Forum Newbie
Posts: 11
Joined: Mon Mar 05, 2007 12:16 pm

Post 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...
Post Reply