Page 1 of 1

Query into drop down menu list

Posted: Mon Nov 23, 2009 10:06 pm
by x86phre3x
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.

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;
}
 
But I get
Warning: Invalid argument supplied for foreach() in /path/to/public_html/crm/global.php on line xxx
error in the form.

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>
 
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:

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;
}
Could somebody please help? 8)

Re: Query into drop down menu list

Posted: Thu Nov 26, 2009 2:30 am
by x86phre3x
Anyone?

Re: Query into drop down menu list

Posted: Thu Nov 26, 2009 2:33 am
by mupparion

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;
}
 
try that instead of the first block of code you posted