问题
基本上我有一个游戏,其中两支球队相互竞争。通过第一个while循环,我获得了这些团队名称和信息。在第二个循环中,我将获取参与这些团队的用户名/成员,然后运行查询以获取其记录。
我已经习惯了最后一次循环计算,即如果A队有2名球员被命名为 Joe和Mike 而B队有3名球员被命名为 John,Jack和Dave 所以对于A队来说,乔跑了5公里,迈克跑了7公里,这为A队带来了<12>公里。同样,对于B队约翰跑了11公里,杰克做了2公里,戴夫做了1公里,这对于B队来说总共14公里。但由于这个循环显示 A队= 12km 和 B队= 26km 。如果有人能引导我走向可能出错的地方,那将是很棒的。
代码
$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>';
答案 0 :(得分:0)
您需要在循环之前重置$activityTotal
。
在第一个循环后,它的值是12公里,并且
然后你增加14公里,它变成26公里而不是14公里。
校正:
$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 />";
}
答案 1 :(得分:0)
$activityTotal set to empty or 0 before next team total start
//use
$activityTotal = 0; //init to 0 before loop