我有一个名为“项目”的表
id month year **itemname** distmoney
1 12 2012 chicken 20
2 12 2012 pork 15
3 11 2012 chicken 21
4 11 2012 pork 15
我试图找出两个月之间相同项目名称的“distmoney”之间的区别。
例子:
ID 1,物品名称 chicken。第 12 个月的 distmoney 是 20,而第 11 个月的 distmoney 是 21。我希望能够计算 id=1、itemname=chicken 的差值 1
现在我有 php 代码来计算两个数字之间的差异,但我很难弄清楚如何抓住上个月的 distmoney。
<?php foreach($rows as $row): ?>
<?php $number1 = htmlentities($row['distmoney']) ?>
<?php endforeach; ?>
<?php
$number1 = $row['distmoney'];
$number2 = ????????; // THIS NEEDS TO BE THE PREVIOUS MONTH DISTMONEY VALUE
if ($number1 <= $number2) {
$difference = "(Price Lowered) Price difference of $";
$result = $number2 - $number1;
$percent = round(100.0*($number2-$number1)/$number1);
echo $difference; echo $result; echo $percent; echo "%";
} elseif ($number1 > $number2) {
$result = $number1 - $number2;
$percent = round(100.0*($number2/$number1-1));
$addition = "(Price Higher) Price difference of $";
echo $addition; echo $result; echo $percent; echo "%";
}
?>