Query into drop down menu list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
x86phre3x
Forum Newbie
Posts: 2
Joined: Mon Nov 23, 2009 9:51 pm

Query into drop down menu list

Post 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)
x86phre3x
Forum Newbie
Posts: 2
Joined: Mon Nov 23, 2009 9:51 pm

Re: Query into drop down menu list

Post by x86phre3x »

Anyone?
mupparion
Forum Newbie
Posts: 15
Joined: Tue Sep 22, 2009 3:48 am

Re: Query into drop down menu list

Post 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
Post Reply