Dropdown box containing values from database.

A

Anonymous

Guest
Sorry for the late response. Just in case you haven't gotten it sorted out yet, this one is kind of a common issue. For re-usability purposes, i typically write it into a function. Here's a quick example. adding an 'id' parameter that automatically sets an option based on a passed id is trivial, but a good idea. I'll leave that as an exercise for you.
PHP:
<?php
function createUserDropdown(){
    $db=  Database::getInstance();
    $sql = "SELECT id, firstName,lastName FROM Employee";
    $result = $db->query($sql);
    if ((! $result)||count($result)==0){
        die('failed to pull employee list'.$db->error);
    }
    $output = '<select name="user"><option>Pick your name</option>';
    while ($row = $result->fetch()){
        
        $output .= '<option value='. $row['id'] . '>'. $row['firstName']. ' ' . $row['lastName'] .'</option>';
    }
    $output .= '</select>';
    return $output;
}
 
 
Back
Top