dqoys62082 2014-02-05 12:35
浏览 25

foreach循环给出意想不到的结果

I have a table of users and a table of scores in golf tournaments for those users. When a user plays a tournament he records a score in the results table using a form. I want to display a table of results showing the full list of users and the scores in the tournaments. There are eight columns of scores in the table - one for each course played. I am struggling with the php code to display the resulting scores. Where a player has played, his score appears correctly but if the following player in the table has not played his score is shown as the score for the player above him in the table. This continues down the list until a genuine score is encountered. I have tried to find the answer to this without success. Here is my code for just a single course. If I can overcome the problem for this I am sure the code will work for the other courses.

$query = "SELECT * FROM emg_users ORDER BY user_login";
$results=mysql_query($query);
$results_array = array();
while ($row = mysql_fetch_array($results)) {
$results_array[$row['ID']] = $row;
}
    foreach ($results_array as $id => $record) { 
    $player = $record['user_login'];
?>
        <tr>
            <td class="padded_c" nowrap="nowrap"><?php echo $player;?></td> <!-- Player    name -->
<?php
    $query = "SELECT * FROM 0_tournament_test WHERE player = '".$player."' AND course = 'St_Andrews'";
    $results=mysql_query($query);
    $results_array = array();
    while ($row = mysql_fetch_array($results)) {
    $results_array[$row['id']] = $row;
    }
    foreach ($results_array as $id => $record) {
    $pplayer = $record['player'];
        if ($pplayer = $player) {
            $sta = $record['score'];
            } 
        else {
            unset($sta);
            }
    $id++;
    }
?>
        <td class="padded_c" nowrap="nowrap"></td>
        <td  nowrap="nowrap"><?php echo $sta;?></td>
        <td  nowrap="nowrap"><?php echo $rsg;?></td>
        <td  nowrap="nowrap"><?php echo $bet;?></td>
        <td  nowrap="nowrap"><?php echo $oak;?></td>
        <td  nowrap="nowrap"><?php echo $kia;?></td>
        <td  nowrap="nowrap"><?php echo $cng;?></td>
        <td  nowrap="nowrap"><?php echo $mer;?></td>
        <td  nowrap="nowrap"><?php echo $oly;?></td>
        <td  nowrap="nowrap"><?php echo $allshots;?></td>
        <td  nowrap="nowrap"><?php echo $sss;?></td>
        <td  nowrap="nowrap"><?php echo '';?></td>
    </tr>
<?php
$id++;
}
?>
    </table>
  • 写回答

1条回答 默认 最新

  • douyue5856 2014-02-05 12:40
    关注

    I think you forgot some login...

    Little Change in your code :--

    if ($pplayer == $player) {
        $sta = $record['score'];
    } 
    else
    {
        unset($sta);
    }
    

    Add this condition to your code...

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line