0

I am trying to create a drop down menu of usernames or a <select> as it's known in HTML. However I am only getting the last value back from my array and I can't figure out why.

PHP

function getUserName($db) {

    try {
        $sql = 'SELECT members.name FROM 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 as $key => $value) { ?>
    <option value="<?php echo $key ?>"><?php echo $value ?></option>
  <?php }?>
</select>

I'm fairly sure the HTML section of my code is solid. I think the error lies in how I'm adding values to my $names array but after staring at it for a half an hour I can't see it. Thanks for any help/fresh eyes.

4 Answers 4

3

You need to declare your array outside the loop

if ($count > 0) {
    $names = array(); 
    while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
        $names[] = $row['name'];                                
    }
    return $names;          
}      
Sign up to request clarification or add additional context in comments.

Comments

2

this is emptying your array every time : $names = array();

use this :

while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
    $names[] = $row['name'];                                
}

Comments

2

You are creating a new $names array every row then returning the last one. You need to declare the array outside the while loop. The following should work:

function getUserName($db) {

try {
    $sql = 'SELECT members.name FROM members';
    $query_an = $db->query($sql);               
    $count = $query_an->rowCount();

    if ($count > 0) {
        $names = array();
        while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
            $names[] = $row['name'];                                
        }
        return $names;          
    }       
} catch(PDOException $e) {
    die($e->getMessage());
}
}

Comments

2

The problem is you're re-initializing the array on every loop:

See:

while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
                $names = array();   
                $names[] = $row['name'];                                
            }
            return $names; 

Should be:

$names = array(); 
 while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {                      
                $names[] = $row['name'];                                
            }
            return $names;     

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.