Jump to content


Photo

performing math on fetched rows


  • Please log in to reply
2 replies to this topic

#1 chaulkman

chaulkman
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 June 2006 - 01:20 PM

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];]

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 11 June 2006 - 01:33 PM

Don't format numbers that you want to use in calculations, format on output.

$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];
}    

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 chaulkman

chaulkman
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 11 June 2006 - 03:01 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users