Tony Scott
Tony Scott

Reputation: 1

foreach loops giving unexpected results

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>

Upvotes: 0

Views: 50

Answers (1)

Akshay Paghdar
Akshay Paghdar

Reputation: 3629

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...

Upvotes: 1

Related Questions