PHP & MySQL - Populate Select Dropdown using Arrays

82
January 05, 2019, at 07:10 AM

I'm trying to populate my dropdown using an array on another page so here is what I've got I need to populate my dropdown select a list of nationalities, so far I've got an array of list of nationalities but I cannot populate the select box on the array.

on my fetch.php

I've got

<?php include('nationality.php') ?>

<select id="register-nationality" name="registerNationality" required data-msg="Please select your Nationality" class="form-control">
                    <option>Select Nationality</option>
                    <?php 
                    foreach ($nationality as $national) {
                      echo "<option value='$nationals'>$nationals</option>";  
                    }
                     ?>
                  </select> 

and in the nationality.php here is where I got the list of nationalities and it is an array.

so here is a sample of the code it is too long so I will give you the sample

    <?php 
$nationals = array(
        'Afghan',
        'Albanian',
        'Algerian',
        'American',
        'Andorran',
        'Angolan',
        'Antiguans',
        'Argentinean',
        'Armenian',
        'Australian',
        'Austrian',
        'Azerbaijani',
        'Bahamian',
        'Bahraini',
        'Bangladeshi',
        'Barbadian',
        'Barbudans',
..
..
..  and so on
);
 ?>

and so nothing is showing.

Answer 1

Change your include statement to : include_file "nationality.php";

The name of your array is $nationals. You can't use $nationality because it doesn't exists.

Change your code to the following:

foreach($nationals as $national){
    echo "<option value='$national'>$national</option>";
}
Answer 2

Yor foreach variable is $nationals and not $nationality. you have used $nationals which is not defined. your variable name is $national. in your loop change as echo "<option value='$national'>$national</option>";

$nationals = array(
    'Afghan',
    'Albanian',
    'Algerian',
    'American',
    'Andorran',
    'Angolan',
    'Antiguans',
    'Argentinean',
    'Armenian',
    'Australian',
    'Austrian',
    'Azerbaijani',
    'Bahamian',
    'Bahraini',
    'Bangladeshi',
    'Barbadian',
    'Barbudans',
);
foreach ($nationals as $national) {
  echo "<option value='$national'>$national</option>";  
}
READ ALSO
MYSQL find all companies with the same parent company

MYSQL find all companies with the same parent company

I have the following MYSQL table with three columnscompany_id is the primary key column

82
Wrong value inserted into oauth_access_token table

Wrong value inserted into oauth_access_token table

I'm setting up Laravel passport in my Laravel application and it is not working as intended when I perform a Request::create to /oauth/token and Laravel/Passport simply insert my username to the wrong column (user_id) in table oauth_access_tokens

245
How can I convert this php mysql code to SQL? I am trying to store values in an array [on hold]

How can I convert this php mysql code to SQL? I am trying to store values in an array [on hold]

I have this php code which executes mysql queries, I want to build sql code for my databaseI am trying to get all the values from a mySQL column and store them in an array

98