99Botch
99Botch

Reputation: 43

How can I put my array of countries into a <select> tag?

So, I'm doing a form in a mix html/php project were I want to have a select tag that gives as options the all list of countries. I stored my list under an array "countries" as such:

$countries = array("Afghanistan", "Albania", "Algeria", ...."Zambia", "Zimbabwe");

and tried to loop within my select tag so that each country would pop up into a option tag.

<div class="form-group">
                            <label for="countries" class="col-md-3 control-label">Countries</label>
                            <div class="col-md-6">
                                <select class="form-control" id="countries" name="countries">
                                    <?php foreach ($countries as $country) { ?>
                                        <option value="<?= $countries->country ?>"><?= $countries->country ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                            <div class="col-md-3 error">
                                <?php error('countries'); ?>
                            </div>
                        </div>

but what I get is "trying to get property of non-object"

So because I'm not advance in php, I'm kinda blocked with what's happening if anyone is willing to help me. Thank you

Upvotes: 1

Views: 1394

Answers (4)

dandemo
dandemo

Reputation: 397

Another way is to just use a for loop. Given the array $countries, you can do this:

<select name="country">
<?php
$ct = count($countries)
for($i=0;$i<=$ct;$i++){
$country = $countries[$i];
echo "<option value='$country'>$country</option>";
}
?>
</select>

Hope this helps.

Upvotes: 0

PaulS
PaulS

Reputation: 940

If you loop $countries as $country in a foreach, each element of $countries is saved in $country in every instance of the loop.

That means, to print each country, a loop looks like this:

foreach ($countries as $country) {
  echo $country;
}

Upvotes: 0

Alberto
Alberto

Reputation: 12939

obviously this is the wrong part:

<?php foreach ($countries as $country) { ?>
    <option value="<?= $countries->country ?>"><?= $countries->country ?></option>
<?php } ?>

because foreach ($countries as $country) means "go inside $countries and save on each iteration the current element inside $country", and so the code should looks like this:

<?php foreach ($countries as $country) { ?>
    <option value="<?= $country ?>"><?= $country ?></option>
<?php } ?>

Upvotes: 0

mail2bapi
mail2bapi

Reputation: 1617

$countries is an array and you are using foreach loop which gets the value of array to $country. You just to echo $country.

<div class="form-group">
       <label for="countries" class="col-md-3 control-label">Countries</label>
       <div class="col-md-6">
            <select class="form-control" id="countries" name="countries">
                  <?php foreach ($countries as $country) { ?>
                       <option value="<?= $country ?>"><?= $country ?></option>
                  <?php } ?>
              </select>
        </div>
        <div class="col-md-3 error">
              <?php error('countries'); ?>
        </div>
</div>

Hope this helps you :)

Upvotes: 2

Related Questions