Query into drop down menu list
Posted: Mon Nov 23, 2009 10:06 pm
Hi all,
I am new to PHP. I am trying to get a result from a query and list it into a drop down menu. Below is my current code in the global.php. This is the code from Deadlock PHP application which I am trying to modify for my web.
But I get
Below is the code in the form:
I think the query result somehow needs to be converted into array first because the original code to select country does use PHP basic array for the selection. Please refer below.
Example Code:
Could somebody please help? 
I am new to PHP. I am trying to get a result from a query and list it into a drop down menu. Below is my current code in the global.php. This is the code from Deadlock PHP application which I am trying to modify for my web.
Code: Select all
function agent_menu ($selected_agent){
$agentsquery = ("SELECT username FROM agents");
$menu_code4 = '<select name="agent">'."\n";
foreach ($$agentsquery as $agent){
if($selected_agent == $agent) $select_text = ' selected="selected_agent"'; else $select_text = NULL;
$menu_code4 .= '<option value="'.$agent.'"'.$select_text.'>'.htmlentities($agent).'</option>'."\n";
$menu_code4 .= '</select_agent>';
}
return $menu_code4;
}
error in the form.Warning: Invalid argument supplied for foreach() in /path/to/public_html/crm/global.php on line xxx
Below is the code in the form:
Code: Select all
<tr>
<td width="50%" class="style5">Agent Name:</td>
<td><?php
if(isset($_POST['agent'])){
print dealer_menu($_POST['agent']);
} else {
print dealer_menu("Please select");
}
?></tr>
Example Code:
Code: Select all
function state_menu ($selected_state){
$states = array(
"Please Select",
"State 1",
"State 2",
"State 3",
"State 4",);
$menu_code3 = '<select name="state">'."\n";
foreach ($states as $state){
if($selected_state == $state) $select_text = ' selected="selected_state"'; else $select_text = NULL;
$menu_code3 .= '<option value="'.$state.'"'.$select_text.'>'.htmlentities($state).'</option>'."\n";
}
$menu_code3 .= '</select_state>';
return $menu_code3;
}