0

I have a multi-select box for country selection. I want to select any countries which are associated, meaning an array I get from the database.

Here's the code I have:

                        <?php
                        foreach($countries as $country){
                            if(!empty($offer_countries)){
                                foreach($offer_countries as $key => $offer_country){
                                    if(isset($offer_country['country_id']) && ($offer_country['country_id'] == $country['id'])){
                                        echo '<option value="'.$country['id'].'" selected>'.$country['name'].'</option>';

                                    }else{
                                        echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';

                                    }
                                }
                            }else{

                                echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';

                            }
                        }
                    ?>

The $offer_countries array, looks like this:

Array
(
    [0] => Array
        (
            [country_id] => 1
        )

    [1] => Array
        (
            [country_id] => 2
        )

    [2] => Array
        (
            [country_id] => 3
        )

)

I'm looping all countries to display them, then I have a nested foreach to see if the country is already set, if so, make the option box selected.

The problem with this is that let's say I have 3 items selected, it'll display 3 of the same country, based on the number of items in the array. So if United States should be checked, it'll show it three times, with the last one checked.

Ok, sorry for the looong explanation, it's probably fairly self explanatory, but any help would be awesome!

4
  • Use in_array or array_search instead of the nested foreach. Commented May 12, 2011 at 21:25
  • 1
    in_array and array_search wont work because it's arrays within an array Commented May 12, 2011 at 21:42
  • sorry. I swore there was some way to search recursively. Commented May 12, 2011 at 22:00
  • I suppose array_uintersect could work, but I doubt there'd be a performance improvement in this case. Commented May 12, 2011 at 22:03

2 Answers 2

2

This solved it:

                        <?php
                        foreach($countries as $country){
                            $i = 0;
                            if(!empty($offer_countries)){
                                foreach($offer_countries as $key => $offer_country){
                                    if($offer_country['country_id'] == $country['id']){
                                        echo '<option value="'.$country['id'].'" selected>'.$country['name'].'</option>';
                                        $i = 1;
                                        break;
                                    }
                                }
                                if($i == 0){
                                    echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';
                                }
                            }else{

                                echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';

                            }
                        }
                    ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Your inner 'foreach' statement is going to output 'something' whether or not the value is set, and it does so based on the $country variable set up in the outer foreach loop.

So what happens is that you output on the outer 'foreach' loop once for each time it runs on the inner foreach loop.

2 Comments

How do I fix that? I tried added breaks after each output on the inner loop, but then it only selects the last item in the array
Looks like you solved it while I was busy elsewhere. You actually did it more or less the way I would have suggested, using an external boolean value as a flag to track what happened inside the inner foreach loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.