简体   繁体   中英

PHP foreach loop to populate dropdown list with values from an array of arrays

I have an array of an arrays created using the following code. I'm trying to get this to loop through and populate a dropdown list.

$names = $db->fetchAll("select `name` from `classes`");

This is what is stored in the $names variable.

Array ( [0] => Array ( [name] => Web Design ) [1] => Array ( [name] => Art History ) [2] => Array ( [name] => Gym ) [3] => Array ( [name] => English ) [4] => Array ( [name] => Biology ) [5] => Array ( [name] => 3D Animation ) [6] => Array ( [name] => Tech Disc ) [7] => Array ( [name] => Math ) [8] => Array ( [name] => Dance ) [9] => Array ( [name] => Video Production ) [10] => Array ( [name] => Home Ec ) [11] => Array ( [name] => Government ) [12] => Array ( [name] => Physics ) )

I have this dropdown list created and it work, but it is hand coded for each value in the array. I want to modify this so it 'loops' through all results in the array to create the dropdown.

<label for="per1"></label>
<select name="per1" id="per1">
    <option selected="selected">Choose one</option>
    <option value="<?php echo $names[0]['name'];?>"><?php echo $names[0]['name'];?></option>
    <option value="<?php echo $names[1]['name'];?>"><?php echo $names[1]['name'];?></option>    
    <option value="<?php echo $names[2]['name'];?>"><?php echo $names[2]['name'];?></option>
    <option value="<?php echo $names[3]['name'];?>"><?php echo $names[3]['name'];?></option>
    <option value="<?php echo $names[4]['name'];?>"><?php echo $names[4]['name'];?></option>
    <option value="<?php echo $names[5]['name'];?>"><?php echo $names[5]['name'];?></option>
    <option value="<?php echo $names[6]['name'];?>"><?php echo $names[6]['name'];?></option>
    <option value="<?php echo $names[7]['name'];?>"><?php echo $names[7]['name'];?></option>
    <option value="<?php echo $names[8]['name'];?>"><?php echo $names[8]['name'];?></option>
    <option value="<?php echo $names[9]['name'];?>"><?php echo $names[9]['name'];?></option>
    <option value="<?php echo $names[10]['name'];?>"><?php echo $names[10]['name'];?></option>
    <option value="<?php echo $names[11]['name'];?>"><?php echo $names[11]['name'];?></option>
    <option value="<?php echo $names[12]['name'];?>"><?php echo $names[12]['name'];?></option>           
</select> 

Can someone please help?

This solution works for current PHP versions. Simple case of using a foreach:

<select name="per1" id="per1">
  <option selected="selected">Choose one</option>
  <?php
    foreach($names as $name) { ?>
      <option value="<?= $name['name'] ?>"><?= $name['name'] ?></option>
  <?php
    } ?>
</select> 

This solution works for older PHP versions. Simple case of using a foreach:

<select name="per1" id="per1">
  <option selected="selected">Choose one</option>
  <?php
    foreach($names as $name) { ?>
      <option value="<?php echo $name['name'] ?>"><?php echo $name['name'] ?></option>
  <?php
    } ?>
</select> 

i think this is enough

foreach($names as $key =>$value)
{?>
<option value="<?=$value['name']?>"><?=$value['name']?></option>    
<?php }

Since you don't need to write the options' value attributes with values that are identical to the options' text, this operation can be done with implode instead of a classic loop.

<?php $options = array_column($names, 'name'); ?>
<select name="per1" id="per1">
    <option value="">Choose one</option>
    <?php if ($options) {
        echo '<option>' . implode('</option><option>', $options) . '</option>';
    } ?>
</select>

This question is surely a mega-duplicate, but I didn't yet take the time to search for the earliest posting of this task on Stack Overflow.

Go for for each loop to avoid these much coding.

<?php 
foreach($names as $nameIndex=>$nameVal){ ?>
   <option value=$nameVal['name']?>"><?=$nameVal['name']?> </option>
<?php } ?>

Easiet way to use php array in dropdown html form or anywhere to show php foreach values in as dropdown values

<select name="city" required class="form-control"> 
   <option value="">Select City</option>
   <?php foreach($pkcities as $rows){ $city = $rows['city']; ?>
   <option  value="<?=$city;?>" ><?=$city;?></option>
   <?php } ?>
   </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM