Board index   FAQ   Search  
Register  Login
Board index php forum :: Database phpMyAdmin

Dropdown box containing values from database.

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

Moderators: macek, egami, gesf

Dropdown box containing values from database.

Postby chrisf2012 » Wed Feb 20, 2013 11:02 am

I am new to both MySQL and PHP so I really need help!

I have two tables 'Employee' and 'Holiday'.

in my holiday table I have the field 'employee ID' which is a foreign key from the primary key in the employee table.

I am creating a form where employees book holiday. so in the form you enter employee id, first/last name and date of holiday.

Can I have a drop down box in my holiday form for employee ID, where the values in the drop down box are the employee ID's from the employee table?

I apologise if you can not understand my explanation but I appreciate all help! Thanks
chrisf2012
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Feb 20, 2013 10:58 am

Re: Dropdown box containing values from database.

Postby chrisf2012 » Thu Feb 21, 2013 8:25 am

anyone?
chrisf2012
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Feb 20, 2013 10:58 am

Re: Dropdown box containing values from database.

Postby 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;
}
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to phpMyAdmin

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.