Page 1 of 1

Display database info in dropdown list

Posted: Mon Jul 24, 2006 9:00 am
by rapidz
Hi people.

Could anyone help me here, i need to output two fields from my sql database, and join them together so that the dropdown list in my CMS page displays the first and last name of a member of staff.

the table name is called tbl_staff
the firstname field is called staff_nameFirst
the surname field is called staff_nameLast

All help will be appreciated.

Many thanks,

Nick.

Posted: Mon Jul 24, 2006 9:14 am
by jamiel

Code: Select all

SELECT CONCAT(staff_nameFirst , ' ', staff_nameLast) FROM tbl_staff

Posted: Mon Jul 24, 2006 9:24 am
by bokehman

Code: Select all

function dropdown($options_array, $selected = null)
{
    $return = null;
    foreach($options_array as $option)
    {
        $return .= '<option value="'.$option.'"'.
                   (($option == $selected) ? ' selected="selected"' : null ).
                   '>'.ucwords($option).'</option>'."\n";
    }
    return $return;
}

// DB connect stuff here

$query = "SELECT `staff_nameFirst`, `staff_nameLast` FROM `tbl_staff` ORDER BY `staff_nameLast` ASC";
$result = mysql_query($query) or die($query ."<br>\n". mysql_error());
$names = array();
while($row = mysql_fetch_assoc($result))
{
	$names[] = $row['staff_nameFirst'] .' '. $row['staff_nameLast'];
}
echo '<p><label>Name</label> <select name="name" size="1">'.dropdown($names).'</select></p>'."\n";

Posted: Mon Jul 24, 2006 9:26 am
by rapidz
bokehman, thankyou very much.