Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

loops

php

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?

like image 327
Mr. B Avatar asked May 08 '14 15:05

Mr. B


2 Answers

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> 
like image 102
Josh from Qaribou Avatar answered Nov 02 '22 23:11

Josh from Qaribou


i think this is enough

foreach($names as $key =>$value)
{?>
<option value="<?=$value['name']?>"><?=$value['name']?></option>    
<?php }
like image 44
ɹɐqʞɐ zoɹǝɟ Avatar answered Nov 02 '22 22:11

ɹɐqʞɐ zoɹǝɟ