chaulkman Posted June 11, 2006 Share Posted June 11, 2006 I have a db that tracks the results of a PGA Tour golf pool. My query retrieves two fields - teamName and earnings. The code below then displays the results in a table. In the first col I've added a field called rank which displays the team ranking (1-16) . The next two cols then display the teamName and their earnings. $max equals the earnings of the first place team.With the final two cols I want to calculate (1) the difference between each team and the team in the next highest position and (2) the difference betweeen the current team and the first place team.I've tested and the variables $prevTeam and $currTeam are returning correct values.The problem occurs when I perform the math to calculate $behindLeader and $behindNextTeam. The values returned and displays are small ints (0, 2, 6, etc) instead of the correct values which should be 4,340,00 etc.Could my data types be the problem? Any help would be appreciated.[//Fetch and print all records. $rank = 1; $prevTeam = number_format ($max); //set prevTeam as $max for first pass through loop $behindNextTeam = 0; $behindLeader = 0; while ($row = mysql_fetch_array($result,MYSQL_NUM)){ $row_color = ($rowCount % 2) ? $color1 : $color2; $behindLeader = $max - $row[1]; $currTeam = $row[1]; $behindNextTeam = $prevTeam - $currTeam; echo "<tr> <td bgcolor=\"$row_color\" align=\"left\">$rank</td> <td bgcolor=\"$row_color\" align=\"left\">$row[0]</td> <td bgcolor=\"$row_color\" align=\"right\">$row[1]</td> <td bgcolor=\"$row_color\" align=\"right\">$behindNextTeam</td> <td bgcolor=\"$row_color\" align=\"right\">$behindLeader</td> </tr> \n"; $rowCount++; $rank++; $prevTeam = $row[1];] Quote Link to comment https://forums.phpfreaks.com/topic/11708-performing-math-on-fetched-rows/ Share on other sites More sharing options...
Barand Posted June 11, 2006 Share Posted June 11, 2006 Don't format numbers that you want to use in calculations, format on output.[code]$rank = 1;$behindNextTeam = 0;$behindLeader = 0;while ($row = mysql_fetch_array($result,MYSQL_NUM)){ $row_color = ($rowCount % 2) ? $color1 : $color2; if ($rank==1) { $max = $row[1]; $prevTeam = $row[1]; } $behindLeader = $max - $row[1]; $currTeam = $row[1]; $behindNextTeam = $prevTeam - $currTeam; echo "<tr> <td bgcolor=\"$row_color\" align=\"left\">$rank</td> <td bgcolor=\"$row_color\" align=\"left\">$row[0]</td> <td bgcolor=\"$row_color\" align=\"right\">" . number_format($currTeam,0) . "</td> <td bgcolor=\"$row_color\" align=\"right\">" . number_format($behindNextTeam,0) . "</td> <td bgcolor=\"$row_color\" align=\"right\">" . number_format($behindLeader,0) . "</td> </tr> \n"; $rank++; $prevTeam = $row[1];} [/code] Quote Link to comment https://forums.phpfreaks.com/topic/11708-performing-math-on-fetched-rows/#findComment-44264 Share on other sites More sharing options...
chaulkman Posted June 11, 2006 Author Share Posted June 11, 2006 Thanks that worked. I also had a FORMAT statement in my query definition (code not displayed) that was also causing problems with calculations. Good lesson - no formatting till the end. Thanks also for the more efficient loop logic. Quote Link to comment https://forums.phpfreaks.com/topic/11708-performing-math-on-fetched-rows/#findComment-44287 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.