Dropdown box containing values from database.

How to use phpmyadmin.... phpmyadmin related arguments. Even phpMyAdmin issues

Moderators: macek, egami, gesf

Post Reply
seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: Dropdown box containing values from database.

Post by seandisanti » Fri Mar 01, 2013 12:59 pm

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.

Code: Select all

<?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;
}
 

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests