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 ($position as $pposition) {
    $result = mysql_query("SELECT `player_id`,`name`,`surname` FROM `player_info` WHERE `position` = '$pposition'") or die(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 element

            print $pposition;
            echo '<select>';
            foreach ($playerName as $name) { //assign playername to position element
                echo '<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

function get_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($position as $pposition){
    $data   =   get_players_by_position($pposition);
    echo $pposition;    
    echo '<select>';
    foreach($data as $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"