7

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?

6 Answers 6

19

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> 
Sign up to request clarification or add additional context in comments.

6 Comments

it creates a list, but the options and values are empty. Any ideas? $names is an array of arrays. Does something have to chance because of that?
$names is an array of arrays, so when you foreach it, each 'name' is itself an array, and the array index you need is ['name']. You didn't specify a php version, but if you have a <5.4 version of PHP the short-tags I used may need to be <?php echo (instead of <?= ).
It was the <php echo because I have an older version of php. THANK YOU!
There is absolutely no benefit in mirroring the option's text as its value attribute -- you can safely omit the value declaration. The form submission and all js processes will still work the same. The first option will be selected if no other option is selected.
@mickmackusa that's true, but while there's no 'benefit' those aren't equivalent. My answer intended to output the same HTML. e.g. if they had document.querySelector('#pet-select option[value=Physics]'). somewhere, it won't find that element if it's not set in the value attribute.
|
3

i think this is enough

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

2 Comments

There is absolutely no benefit in mirroring the option's text as its value attribute -- you can safely omit the value declaration. The form submission and all js processes will still work the same.
@mickmackusa absolutely right man, thanks
1

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.

Comments

0

Go for for each loop to avoid these much coding.

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

1 Comment

There is absolutely no benefit in mirroring the option's text as its value attribute -- you can safely omit the value declaration. The form submission and all js processes will still work the same.
0

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>

1 Comment

There is absolutely no benefit in mirroring the option's text as its value attribute -- you can safely omit the value declaration. The form submission and all js processes will still work the same.
-1

loop through the array [1] and get the value of the key 'name' display it in the dropdown

foreach($response['items'] as $key => $value)
{
   echo '<option value="'.$value['name'].'">'.$value['name'].'</option>'; 
}

1 Comment

Hello, please notice this question is more than 7 years old, and the accepted answer is very similar to your solution . Since this answer does not improve the accepted one, I'm voting to delete it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.