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 + 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 Value1 Value2 Value3
1 7250 1000 50
2 9357 2000 100
3 10258 3000 150
4 18790 4000 200

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

The output would be:
ID VALUE1 VALUE2 Value3
1 2107 1000 50
2 901 1000 50
3 8532 1000 50

Does someone knows how to do this in PHP?

Does someone has an example?

TnQ
 
it's just straight maths
Code:
<?php
$sql = "select id, value1, value2, value3 from table";
$result = mysql_query($sql) or die (mysql_error());

while ($row = mysql_fetch_assoc($result)){
	$rows[] = $row;	
}
echo "<table>";
for ($i=1; $i<=count($rows); $i++){
	$r = $i-1;
	echo "<tr>";
	echo "<td>$r</td>";
	for ($j=1; $j<=3; $j++){
		echo "<td>".$rows[$i]['value'.$j] - $rows[$r]['value'.$j] ."</td>";
	}
	echo "</tr>";
}
echo "</table>";

?>
 
Hi jpadie,

You've helped me the last time also with this. ThQ for that.

When i copy the code into my page the following output appears:

000
000
000
000
000
1
2
3
4
5

Thanks you in advance

TT
 
jpadie,

The above question is not really clear.

The output is

000
000
000
000
000
1
2
3
4
5

instead of

1 2107 1000 50
2 901 1000 50
3 8532 1000 50

Greetz TT
 
strange.

this test harness does what i think you want and it is largely the same as previously posted

Code:
<?php
$pdo = new PDO('sqlite::memory:');
$create = "create table if not exists ty (id integer  primary key autoincrement, value1 int(10) NOT NULL, value2 int(10) NOT NULL, value3 int(10) NOT NULL)";
$r = $pdo->exec($create);
if ($r === false) {print_r($pdo->errorinfo());}
$stmnt = $pdo->prepare("insert into ty values (?,?,?,?)");
if (!$stmnt){
	print_r($pdo->errorinfo());
}
for ($i=0; $i<20; $i++){
	$param = array();
	$param[] = null;
	for ($p=0; $p<3; $p++){
		$param[] = rand(0, 100000);
	}	
	$stmnt->execute($param);
}

$sql = "select id, value1, value2, value3 from ty";
foreach ( $pdo->query($sql) as $row){
	$rows[] = $row;
}
echo "<table>";
for ($i=1; $i<count($rows); $i++){
	$r = $i-1;
	echo "<tr>";
	echo "<td>".$rows[$r]['id']."</td>";
	for ($j=1; $j<=3; $j++){
		echo "<td>".($rows[$i]['value'.$j] - $rows[$r]['value'.$j]) ."</td>";
	}
	echo "</tr>";
}
echo "</table>";
?>
 
jpadie,


I've got it working, almost...

It displays the rows only all the values are 0

do i have to change something in the line echo "<td>".($rows[$i]['value'.$j] - $rows[$r]['value'.$j]) ."</td>";

'value' for example???

TnQ TT
 
jpadie,


Now it works. Thank u very much for your help!!!!!!


Greetz TanTrazz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top