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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With