0

I am having a few problems with the following code; I have a select with the option to select multiple options which then get inputted into the DB in a list format, I then convert these into arrays when I need to cross reference if they are selected for help I have included the var_dump for both $countryList and $DBcountries the area I am having issues with is

foreach ($DBcountries as $country) this foreach statement appears to be not working correctly.

Check below for both $countryList and $DBcountries ; Thanks.

The problem I have is that at the moment it doesn't select all the selected options that is stored in the DB.

<?php
        
        $countryList = file('/var/www/vhosts/example.co.uk/httpdocs/user/tools/countries.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
                
        $options = '';

        $DBcountries = $fetch['locations'];
        $DBcountries = explode(', ', $DBcountries);
                    
        foreach ($countryList as $country) {

        foreach ($DBcountries as $cross){
            
            if($cross == $country){
                $selected = 'selected';
            }else{
                $selected = '';
            }
            
        }

        if($DBcountries == 'All'){
            $all = 'selected'; 
        }               
            $options .= '<option value="'.$country.'" ' . $selected . '>'.$country.'</option>';
        }
        
        $select = '<select class="form-select" oninput="locationOutputUpdate(value)" id="country" name="location" multiple aria-label="multiple select example"><option ' . $all . ' >All</option>'.$options.'</select>';

        echo $select;
        
        ?>

vardump of $DBcountries ;

array(2) {
  [0]=>
  string(11) "Afghanistan"
  [1]=>
  string(7) "Albania"
}

vardump of $countryList ;

array(196) {
  [0]=>
  string(11) "Afghanistan"
  [1]=>
  string(7) "Albania"
  [2]=>
  string(7) "Algeria"
  [3]=>
  string(7) "Andorra"
  [4]=>
  string(6) "Angola"
  [5]=>
  string(14) "Antigua & Deps"
  [6]=>
  string(9) "Argentina"
  [7]=>
  string(7) "Armenia"
  [8]=>
  string(9) "Australia"
  [9]=>
  string(7) "Austria"
  [10]=>
  string(10) "Azerbaijan"
  [11]=>
  string(7) "Bahamas"
  [12]=>
  string(7) "Bahrain"
  [13]=>
  string(10) "Bangladesh"
  [14]=>
  string(8) "Barbados"
  [15]=>
  string(7) "Belarus"
  [16]=>
  string(7) "Belgium"
  [17]=>
  string(6) "Belize"
  [18]=>
  string(5) "Benin"
  [19]=>
  string(6) "Bhutan"
  [20]=>
  string(7) "Bolivia"
  [21]=>
  string(18) "Bosnia Herzegovina"
  [22]=>
  string(8) "Botswana"
  [23]=>
  string(6) "Brazil"
  [24]=>
  string(6) "Brunei"
  [25]=>
  string(8) "Bulgaria"
  [26]=>
  string(7) "Burkina"
  [27]=>
  string(7) "Burundi"
  [28]=>
  string(8) "Cambodia"
  [29]=>
  string(8) "Cameroon"
  [30]=>
  string(6) "Canada"
  [31]=>
  string(10) "Cape Verde"
  [32]=>
  string(19) "Central African Rep"
  [33]=>
  string(4) "Chad"
  [34]=>
  string(5) "Chile"
  [35]=>
  string(5) "China"
  [36]=>
  string(8) "Colombia"
  [37]=>
  string(7) "Comoros"
  [38]=>
  string(5) "Congo"
  [39]=>
  string(22) "Congo {Democratic Rep}"
  [40]=>
  string(10) "Costa Rica"
  [41]=>
  string(7) "Croatia"
  [42]=>
  string(4) "Cuba"
  [43]=>
  string(6) "Cyprus"
  [44]=>
  string(14) "Czech Republic"
  [45]=>
  string(7) "Denmark"
  [46]=>
  string(8) "Djibouti"
  [47]=>
  string(8) "Dominica"
  [48]=>
  string(18) "Dominican Republic"
  [49]=>
  string(10) "East Timor"
  [50]=>
  string(7) "Ecuador"
  [51]=>
  string(5) "Egypt"
  [52]=>
  string(11) "El Salvador"
  [53]=>
  string(17) "Equatorial Guinea"
  [54]=>
  string(7) "Eritrea"
  [55]=>
  string(7) "Estonia"
  [56]=>
  string(8) "Ethiopia"
  [57]=>
  string(4) "Fiji"
  [58]=>
  string(7) "Finland"
  [59]=>
  string(6) "France"
  [60]=>
  string(5) "Gabon"
  [61]=>
  string(6) "Gambia"
  [62]=>
  string(7) "Georgia"
  [63]=>
  string(7) "Germany"
  [64]=>
  string(5) "Ghana"
  [65]=>
  string(6) "Greece"
  [66]=>
  string(7) "Grenada"
  [67]=>
  string(9) "Guatemala"
  [68]=>
  string(6) "Guinea"
  [69]=>
  string(13) "Guinea-Bissau"
  [70]=>
  string(6) "Guyana"
  [71]=>
  string(5) "Haiti"
  [72]=>
  string(8) "Honduras"
  [73]=>
  string(7) "Hungary"
  [74]=>
  string(7) "Iceland"
  [75]=>
  string(5) "India"
  [76]=>
  string(9) "Indonesia"
  [77]=>
  string(4) "Iran"
  [78]=>
  string(4) "Iraq"
  [79]=>
  string(18) "Ireland {Republic}"
  [80]=>
  string(6) "Israel"
  [81]=>
  string(5) "Italy"
  [82]=>
  string(11) "Ivory Coast"
  [83]=>
  string(7) "Jamaica"
  [84]=>
  string(5) "Japan"
  [85]=>
  string(6) "Jordan"
  [86]=>
  string(10) "Kazakhstan"
  [87]=>
  string(5) "Kenya"
  [88]=>
  string(8) "Kiribati"
  [89]=>
  string(11) "Korea North"
  [90]=>
  string(11) "Korea South"
  [91]=>
  string(6) "Kosovo"
  [92]=>
  string(6) "Kuwait"
  [93]=>
  string(10) "Kyrgyzstan"
  [94]=>
  string(4) "Laos"
  [95]=>
  string(6) "Latvia"
  [96]=>
  string(7) "Lebanon"
  [97]=>
  string(7) "Lesotho"
  [98]=>
  string(7) "Liberia"
  [99]=>
  string(5) "Libya"
  [100]=>
  string(13) "Liechtenstein"
  [101]=>
  string(9) "Lithuania"
  [102]=>
  string(10) "Luxembourg"
  [103]=>
  string(9) "Macedonia"
  [104]=>
  string(10) "Madagascar"
  [105]=>
  string(6) "Malawi"
  [106]=>
  string(8) "Malaysia"
  [107]=>
  string(8) "Maldives"
  [108]=>
  string(4) "Mali"
  [109]=>
  string(5) "Malta"
  [110]=>
  string(16) "Marshall Islands"
  [111]=>
  string(10) "Mauritania"
  [112]=>
  string(9) "Mauritius"
  [113]=>
  string(6) "Mexico"
  [114]=>
  string(10) "Micronesia"
  [115]=>
  string(7) "Moldova"
  [116]=>
  string(6) "Monaco"
  [117]=>
  string(8) "Mongolia"
  [118]=>
  string(10) "Montenegro"
  [119]=>
  string(7) "Morocco"
  [120]=>
  string(10) "Mozambique"
  [121]=>
  string(16) "Myanmar, {Burma}"
  [122]=>
  string(7) "Namibia"
  [123]=>
  string(5) "Nauru"
  [124]=>
  string(5) "Nepal"
  [125]=>
  string(11) "Netherlands"
  [126]=>
  string(11) "New Zealand"
  [127]=>
  string(9) "Nicaragua"
  [128]=>
  string(5) "Niger"
  [129]=>
  string(7) "Nigeria"
  [130]=>
  string(6) "Norway"
  [131]=>
  string(4) "Oman"
  [132]=>
  string(8) "Pakistan"
  [133]=>
  string(5) "Palau"
  [134]=>
  string(6) "Panama"
  [135]=>
  string(16) "Papua New Guinea"
  [136]=>
  string(8) "Paraguay"
  [137]=>
  string(4) "Peru"
  [138]=>
  string(11) "Philippines"
  [139]=>
  string(6) "Poland"
  [140]=>
  string(8) "Portugal"
  [141]=>
  string(5) "Qatar"
  [142]=>
  string(7) "Romania"
  [143]=>
  string(18) "Russian Federation"
  [144]=>
  string(6) "Rwanda"
  [145]=>
  string(16) "St Kitts & Nevis"
  [146]=>
  string(8) "St Lucia"
  [147]=>
  string(30) "Saint Vincent & the Grenadines"
  [148]=>
  string(5) "Samoa"
  [149]=>
  string(10) "San Marino"
  [150]=>
  string(19) "Sao Tome & Principe"
  [151]=>
  string(12) "Saudi Arabia"
  [152]=>
  string(7) "Senegal"
  [153]=>
  string(6) "Serbia"
  [154]=>
  string(10) "Seychelles"
  [155]=>
  string(12) "Sierra Leone"
  [156]=>
  string(9) "Singapore"
  [157]=>
  string(8) "Slovakia"
  [158]=>
  string(8) "Slovenia"
  [159]=>
  string(15) "Solomon Islands"
  [160]=>
  string(7) "Somalia"
  [161]=>
  string(12) "South Africa"
  [162]=>
  string(11) "South Sudan"
  [163]=>
  string(5) "Spain"
  [164]=>
  string(9) "Sri Lanka"
  [165]=>
  string(5) "Sudan"
  [166]=>
  string(8) "Suriname"
  [167]=>
  string(9) "Swaziland"
  [168]=>
  string(6) "Sweden"
  [169]=>
  string(11) "Switzerland"
  [170]=>
  string(5) "Syria"
  [171]=>
  string(6) "Taiwan"
  [172]=>
  string(10) "Tajikistan"
  [173]=>
  string(8) "Tanzania"
  [174]=>
  string(8) "Thailand"
  [175]=>
  string(4) "Togo"
  [176]=>
  string(5) "Tonga"
  [177]=>
  string(17) "Trinidad & Tobago"
  [178]=>
  string(7) "Tunisia"
  [179]=>
  string(6) "Turkey"
  [180]=>
  string(12) "Turkmenistan"
  [181]=>
  string(6) "Tuvalu"
  [182]=>
  string(6) "Uganda"
  [183]=>
  string(7) "Ukraine"
  [184]=>
  string(20) "United Arab Emirates"
  [185]=>
  string(14) "United Kingdom"
  [186]=>
  string(13) "United States"
  [187]=>
  string(7) "Uruguay"
  [188]=>
  string(10) "Uzbekistan"
  [189]=>
  string(7) "Vanuatu"
  [190]=>
  string(12) "Vatican City"
  [191]=>
  string(9) "Venezuela"
  [192]=>
  string(7) "Vietnam"
  [193]=>
  string(5) "Yemen"
  [194]=>
  string(6) "Zambia"
  [195]=>
  string(8) "Zimbabwe"
}
1
  • 2
    I can't help thinking this would be easier with an array function, such as array_search() instead of your inner foreach(). I suspect the problem is that you should really break out of the inner foreach() as soon as you find the $cross value, because as soon as you check the next value in $DBCountries it won't match and it will overwrite your $selected value. Commented Aug 10, 2021 at 17:27

1 Answer 1

2

Try to replace this bit:

    foreach ($DBcountries as $cross){
        
        if($cross == $country){
            $selected = 'selected';
        }else{
            $selected = '';
        }
        
    }

By this:

    foreach ($DBcountries as $cross){
        
        $selected = '';
        if($cross == $country) {
            $selected = 'selected';
        }

    }

This way all the countries should be selected. The reason is that even if you set a country to selected you overwrite that again in the next iteration of the loop.

Somewhat more efficient would be this:

    foreach ($DBcountries as $cross){
        
        $selected = '';
        if($cross == $country) {
            $selected = 'selected';
            break;
        }

    }

Break out of the loop when the country is found. But shorter would be:

    $selected = in_array($country, $DBcountries) ? 'selected' : '';

Yes, that's all that's needed to replace that loop.

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

1 Comment

Thankyou very much! I really appreciate that. I appreciate you explaining the different ways as well.

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.