DIGIT-ARC read-only archive · snapshot: July 23, 2026

need to substract data current row-previous row in php

1 post · originally from https://geek.digit.in/community/threads/need-to-substract-data-current-row-previous-row-in-php.149238/

A
abhijeet2021 Original post #1
Nov 25, 2011 at 9:47 PM
while ($db_fields = pg_fetch_assoc($results)) {
if(date("Y-m-d")==$db_fields['dt'])
{
break;
}else
{
print "<tr><td>" . $db_fields['dt'] . "</td>";
print "<td>" . $db_fields['count'] . "</td></tr>";
}
}

as you can see in the sample code the loop gives me data somewhat like this

Date Count
2011-1-1 200
2011-1-2 250
2011-1-3 300
2011-1-4 150

now i want to insert a code in the while loop so that i get a custom 3rd column with output something like this


Date Count Diff
2011-1-1 200
2011-1-2 250 50
2011-1-3 310 60
2011-1-4 150 -150

kindly someone help me with the logic behind it
all coding is being done in php