php – How can I put my array of countries into a <select> tag?-ThrowExceptions

Exception or error:

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

How to solve:

$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 🙂

###

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 } ?>

###

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;
}

###

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.

###

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

    ---------------Print Using For Each Loop-------------------

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

    ---------------Print Using For Loop-------------------

    $count=count($countries);

    for($x=0;$x<$count;$x++)
    {
       echo $countries[$x];
    }

Leave a Reply

Your email address will not be published. Required fields are marked *