PHP foreach loop to populate dropdown list with va

2020-02-08 21:50发布

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?

标签: php arrays loops
3条回答
迷人小祖宗
2楼-- · 2020-02-08 21:55

i think this is enough

foreach($names as $key =>$value)
{?>
<option value="<?=$value['name']?>"><?=$value['name']?></option>    
<?php }
查看更多
beautiful°
3楼-- · 2020-02-08 21:58

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> 
查看更多
Viruses.
4楼-- · 2020-02-08 21:59

Go for for each loop to avoid these much coding.

<?php 
foreach($names as $nameIndex=>$nameVal){ ?>
   <option value=$nameVal['name']?>"><?=$nameVal['name']?> </option>
<?php } ?>
查看更多
登录 后发表回答