Problem
Basically I have got a game where 2 teams are competing with each other. With first while loop I get those team names and information. In the second loop I am fetching username/members who are participating in those teams and then run a query to fetch their records.
The final while loop I have used to Some up the teams calculation, ie if Team A have 2 players named as Joe and Mike and Team B have 3 players named as John,Jack and Dave So for Team A Joe did ran 5 km and Mike ran 7 km which brings up to a total of 12 km for team A . Likewise for team B John ran 11km, Jack did 2 km and Dave did 1km which makes a total of 14km for team B . But because of this loop is shows Team A = 12km and Team B = 26km . It will be great if anyone can guide me through to where I might be going wrong.
Code
$t_name = "Joe Running8 Vs Mike Running2";
$sql_team = 'SELECT * FROM teams WHERE t_name="'.$t_name.'" AND game_type = "Activity"';
$result_team = mysql_query($sql_team) or die(mysql_error());
$totalNoUsers = '';
$activityTotal = '';
$avgPP = '';
//Table structure for displaying the results
echo '<table border="1" align="center" width="100%">';
echo '<tr>';
echo '<th>Team Name</th>';
echo '<th>Total no Activity</th>';
echo '<th>Total Users</th>';
echo '<th>Avg per User</th>';
echo '<th>Avg %</th>';
echo '</tr>';
while($row_team = mysql_fetch_array($result_team)){
$sql_SelUsers = 'SELECT * FROM teams where t_name="'.$row_team['team'].'"';
echo $sql_SelUsers."<hr>";
$result_SelUsers = mysql_query($sql_SelUsers) or die(mysql_error());
$totalNoUsers = mysql_num_rows($result_SelUsers);
while($row_SU = mysql_fetch_array($result_SelUsers)){
//$accepted_on = "30/01/2013";
$userA = explode("/","27/01/2014");
$accepted_on = mktime(0,0,0, $userA[1],$userA[0],$userA[2]);
$date_time_compare = date('D, j M Y H:i:s', $accepted_on);
$sql = 'SELECT * FROM data_feeds where username="'.$row_SU['username'].'" AND gadget_data_type= "Activity" and gadget_name = "'.$row_SU['gadget_type'].'" and gadget_sub_data_type = "'.$row_SU['game_parameter'].'" and STR_TO_DATE( date_time, "%a, %e %b %Y %H:%i:%s" ) >= STR_TO_DATE( "'.$date_time_compare.'", "%a, %e %b %Y %H:%i:%s" ) ORDER BY df_id DESC';
$result_df = mysql_query($sql) or die(mysql_error());
echo $row_SU['username']."<br />";
/****** Here is the problem ********/
while($row_df = mysql_fetch_array($result_df)){
$activityTotal += $row_df['gadget_data_type_value'];
echo "<br /><strong>".$activityTotal."</strong><br />";
}
}//end while query data_feeds
echo "TnP-> ".$totalNoUsers;
$activityTotal = $activityTotal/1000;
$avgPP = ($activityTotal/$totalNoUsers);
echo '<tr>';
echo '<td>'.$row_team['challToTeam'].'</td>';
echo '<td>'.number_format($activityTotal, 2, '.', ',').'</td>';
echo '<td>'.$totalNoUsers.'</td>';
echo '<td>'.number_format($avgPP, 2, '.', ',').'</td>';
echo '<td>'.$totalNoUsers.'</td>';
echo '</tr>';
}//end while query Teams
echo '</table>';
You need to reset $activityTotal
before your loop. After the first loop its value is 12 km, and then you add 14 km to it, which becomes 26km instead of 14 km .
Correction:
$activeTotal = 0 // RESET TO ZERO
while($row_df = mysql_fetch_array($result_df)){
$activityTotal += $row_df['gadget_data_type_value'];
echo "<br /><strong>".$activityTotal."</strong><br />";
}
$activityTotal set to empty or 0 before next team total start
//use
$activityTotal = 0; //init to 0 before loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.