Jump to content

performing math on fetched rows


chaulkman

Recommended Posts

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];]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.