Skip to content Skip to sidebar Skip to footer

Create Dropdown Menu On Each Loop Assign Database Value To Option Element

Im working on a page for a sports team where the coach can select his team. What im trying to do is: 1) Print different positions 2) Assign next to position name, players who are O

Solution 1:

This could be your problem

foreach ($positionas$pposition) {
    $result = mysql_query("SELECT `player_id`,`name`,`surname` FROM `player_info` WHERE `position` = '$pposition'") ordie(mysql_error());
    while ($row = mysql_fetch_array($result)) { //create arrays$id[]             = $row['player_id'];
        $playerName[]     = $row['name'];
        $playerLastName[] = $row['surname'];
        // print position and open select elementprint$pposition;
            echo'<select>';
            foreach ($playerNameas$name) { //assign playername to position elementecho'<option>' . $name;
                '</option>';
                echo'</select>';
                echo'<br />';
            } //close assign player nae to position loop

    } //end while  loop
} //end opening for each lo

I have removed foreach ($position as $playerPosition) {

Solution 2:

You can do it like this. the logic is something different

functionget_players_by_position($position)
{
    $query  =   "SELECT
                  `player_id`,
                  `name`,
                  `surname`
                FROM `player_info`
                WHERE `position` = $position
                ORDER BY name   ";

    $result =   mysql_query($query);
    $data   =   array();
    while ($row = mysql_fetch_array($result)) { 
        $data[] =   $row;
    }

    return$data;
}   

foreach($positionas$pposition){
    $data   =   get_players_by_position($pposition);
    echo$pposition;    
    echo'<select>';
    foreach($dataas$row){
        echo'<option '.$row['id'].'>' . $row['name'].'</option>';
    }
    echo'</select>';
    echo'<br />';
    unset($data);
}

Post a Comment for "Create Dropdown Menu On Each Loop Assign Database Value To Option Element"