0

I'm trying to pull values from an array, and running into issues with how it is pulling information out of a query to a foreach loop. I want it to display each database entry into the dropdown list, however it will either give me the warning above or if I hardcode to the array it will only show the first index. Here is the code for the variables:

$countryCode = filter_input(INPUT_POST, 'countryCode');
$countryCodeQuery = 'SELECT * FROM countries';
$statements= $db->prepare($countryCodeQuery);
$statements->bindValue(':countryCode',$countryCode);
$statements->execute();
$countryCodes = $statements->fetch();
$statements->closeCursor();

Here is the code for the foreach loop:

<label>Country Code:</label>
    <select name="countryCode">
        <?php $countryCodeL = array($countryCodes['countryCode']) ?>    
    <option value="<?php echo $customerCountryCode;?>"><?php echo $customerCountryCode;?></option>
        <?php foreach ($countryCodes as $countryCodeL) : ?>
           <option value="<?php echo $countryCodeL['countryCode'];?>">
            <?php echo $countryCodeL['countryCode'];?>
             </option>
        <?php endforeach; ?>

I know that the foreach loop is setup as ($array as $value), however I am getting stuck here. Can somebody give me some insight as to the issue at hand?

  • Did you check which line it is about? I can find 'countryCode' in at least 3 places. I'm *guessing* this is about the last of them.. – Torbjörn Stabo Nov 24 '20 at 19:31
  • Add `var_dump()` to figure out when you still have an array, or which variables are strings. Then fix the ambiguous variable names. – mario Nov 24 '20 at 19:36

0 Answers0