我在一个运动队的页面上工作,教练可以选择他的球队。我想做的是:
1)打印不同的位置
2)在位置名称旁边分配,只与位置相关的玩家。 (即如果位置名称是侧翼,则只应在下拉菜单中显示侧翼)
我对上述问题的逻辑是:
现在旁边的下拉菜单应该有不同的位置 他们,包含与位置相关的球员姓名。
//创建位置数组
$position = array(
"THP",
"HKR",
"LH",
"LK4",
"LK5",
"FLH"
);
echo '<form name="slectPlayers" method="post">';
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
foreach ($position as $playerPosition) {
print $playerPosition;
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 print position and open select loop
} //end while loop
} //end opening for each loop
echo '</form>';
不幸的是,我的逻辑错误或我的代码不正确。这是我得到的输出:(注意只有名称Tendai显示在所有下拉菜单中,没有其他名称出现)
如果有人能指出我正确的方向,那我整个上午一直都在苦苦挣扎,我会非常感激
(请注意,上面的图片不包含真实姓名,只是一个虚构的数据库)
答案 0 :(得分:0)
这可能是你的问题
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
我已移除foreach ($position as $playerPosition) {
答案 1 :(得分:0)
你可以这样做。逻辑是不同的
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);
}