-1

I've created a filter for my search results that allows people to filter user searches based on their State from their profile. The problem I am having is that there are duplicate states showing in the drop-down list for users that have the same state.

Here is the current code trying to eliminate that problem.

<div class="state">
    <input type="hidden" name="search_type" value="members">
    <select id="stateDrop" name="state">
        <option value="name">State</option>
        <?php
            $prevState = ""; 
            foreach($users as $user) {
                $state = get_cimyFieldValue($user->ID, 'STATE'); // Grabing their state from their profile page

                if($prevState != $state) {
                    $prevState = $state;
                    echo '<option value="'.$state.'">'.$state.'</option>';
                }
            }
        ?>
    </select>
</div>

This works great when 2 states are right after each other, but when I have something like the below, it will not eliminate the duplicates...

Not working
  -California
  -Washington
  -California

Working
  -California
  -California
  -Washington

What can I do differently to get the results I need and possibly sort it alphabetically? Thanks,

4

Use array_unique()

<?php
        foreach($users as $user) {
            $states[] = get_cimyFieldValue($user->ID, 'STATE'); // Grabing their state from their profile page
        }
        $states = array_unique($states);
    ?>
    <div class="state">
        <input type="hidden" name="search_type" value="members">
        <select id="stateDrop" name="state">
            <option value="name">State</option>
            <?php
            foreach($states as $state) {
                echo '<option value="'.$state.'">'.$state.'</option>';
            }
            ?>
        </select>
    </div>
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.