Taking the value from the top row of a while function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Deaglex
Forum Newbie
Posts: 24
Joined: Thu Feb 24, 2005 10:15 am

Taking the value from the top row of a while function

Post by Deaglex »

I want to have a table with data in that the top row is the main result and every row after it subtracts it's value from the to value, or the greatest value in the column in the Mysql table. so it would be like:
John 100 100
Matt 75 -25
Jimi 50 -50
Bart 25 -75
etc...

Code: Select all

while ($row = mysql_fetch_array($result, MYSQL_NUM))
		{
		// Alternate color schemes.
		
			$row_color = ($row_count % 2) ? $color1 : $color2;
			
			
		echo "<tr bgcolor=\"$row_color\">
		<td align=\"left\" >$row[0]</td>
		<td align=\"left\" >$row[1]</td>
		<td align=\"left\" >$row[2]</td>
		<td align=\"left\" >$row[3]</td>
		<td align=\"center\" >$row[4]</td>
		<td align=\"center\" >$row[5]</td>
		";
		
			$row_count++;
		}
this is what i have so far...

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it can be done entirely in the sql query.

Code: Select all

SELECT `id`, `name`, `score`, `score` - MAX( `field` ) AS `diff` FROM `table` GROUP BY `id` ORDER BY `score`
Post Reply