Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DB Values + Math 1

Status
Not open for further replies.

TanTrazz

Programmer
Aug 18, 2001
54
NL
Hi All,

Question:

Ive got these values in a MYSQL db.

ID Value
1 7250
2 9357
3 10258
4 18790

What i want to do is calculate the differences between 1 and 2, 2 and 3, 3 and 4 etc.

The output would be:
1 2107
2 901
3 8532

Does someone knows how to do this in PHP?

Does someone has an example?

TnQ
 
Code:
$result = mysql_query("select `id`,`value` from table") or die(mysql_error());
while ($row =mysql_fetch_array($result)){
 $vals[$row[0]] = $row[1];
}
$n = count($vals);
for ($i=0; $i<($n-1); $i++){
 //handle last number
 $m = isset($vals[$i+1]) ? $vals[$i+1] - $vals[$i] : $vals[$i]; 
 echo ($i+1) . "\t$m<br/>\r\n";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top