HTML select not inflating correctly from PHP array -


i trying create drop down menu of usernames or <select> it's known in html. getting last value array , can't figure out why.

php

function getusername($db) {      try {         $sql = 'select members.name members';         $query_an = $db->query($sql);                        $count = $query_an->rowcount();          if ($count > 0) {             while ($row = $query_an->fetch(pdo::fetch_assoc)) {                 $names = array();                    $names[] = $row['name'];                                             }             return $names;                   }            } catch(pdoexception $e) {         die($e->getmessage());     } } 

html

<select>                           <?php $names = getusername($db); foreach($names $key => $value) { ?>     <option value="<?php echo $key ?>"><?php echo $value ?></option>   <?php }?> </select> 

i'm sure html section of code solid. think error lies in how i'm adding values $names array after staring @ half hour can't see it. help/fresh eyes.

you need declare array outside loop

if ($count > 0) {     $names = array();      while ($row = $query_an->fetch(pdo::fetch_assoc)) {         $names[] = $row['name'];                                     }     return $names;           }       

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -