Page 1 of 1

Creating a dropdown menu while avoiding duplicate entries

Posted: Thu Nov 23, 2006 5:46 am
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,

Posted: Thu Nov 23, 2006 5:58 am
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...

Posted: Thu Nov 23, 2006 6:03 am
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

Posted: Thu Nov 23, 2006 6:31 am
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/

Posted: Thu Nov 23, 2006 6:39 am
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,

Posted: Thu Nov 23, 2006 6:41 am
by JayBird
sort() will reset the keys

Posted: Thu Nov 23, 2006 6:42 am
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.