Creating a dropdown menu while avoiding duplicate entries

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Creating a dropdown menu while avoiding duplicate entries

Post by impulse() »

Hello.

I'm trying to create a dropdown list of dates which are retrieved from a DB. The problem I'm having is that it's creating multiple dates of the same date. I'm trying to generate an item in a drop down list, but if it already exists then not to add it to the list. Here is the the code so far:

This puts the elements into an array

Code: Select all

while ($res = mysql_fetch_array($query)) { # Puts dates from db into an array
    $element[$i] = $res['dlvdate'];
    $i++;
This generates the dropdown list

Code: Select all

for ($i = 0; $i <= count($element); $i++) { # Creates elements in the dropdown list
    echo "<option value = '$element[$i]'> $element[$i] </option>";
  }
Hopefully somebody could point me in the right direction.

Regards,
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If you build the list as an array first you can use one of two array methods (which one determined by how you build the array.
Method 1: build the array and then perform array_unique on it.

Code: Select all

// Before output
$element=array_unique($element);
Method 2: index the array by the date (most useful for multiple level or when you need to store multiple values in the unique array).

Code: Select all

// While building
$element[$res['dlvdate']]=$res['dlvdate'];
or

Code: Select all

// While building complex unique array
if (isset($dates[$res['dlvdate']]) {
  $element[$res['dlvdate']][]=$value;
} else {
  $element[$res['dlvdate']]=array($value);
}

// later use array_keys to get the array of dates...
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Using array_unique seems more suitable. That's a cool little function, I'll keep that one on mind for future use.

Thank you, Stephen
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You might also let mysql handle the task by adding DISTINCT to the sql statement
SELECT DISTINCT dlvdate FROM xyz
see http://www.sql-tutorial.com/sql-distinct-sql-tutorial/
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I can't imagine that working Volka as I still need it to select everything from the database but only display a single date once in a dropdown list.


Is there a function to re-arrange array index keys so they start at 0 and increment by 1. I ask this because using array_unique has caused a removal of some index keys so when I count the array and run a loop through the count it doesn't reach the end of the loop because the index keys are higher than the count.

Regards,
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

sort() will reset the keys
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

impulse() wrote:I can't imagine that working Volka as I still need it to select everything from the database but only display a single date once in a dropdown list.
Your code snippet says different.
impulse() wrote:Is there a function to re-arrange array index keys so they start at 0 and increment by 1. I ask this because using array_unique has caused a removal of some index keys so when I count the array and run a loop through the count it doesn't reach the end of the loop because the index keys are higher than the count.
Use foreach instead.
Post Reply