math issue
Posted: Mon Apr 04, 2016 9:40 pm
I have been working on this database for a while. It's to calculate my gas usage. I can't figure out how to do the math. I want to subtract the mileage to get the amount of miles I drove on each tank of gas. Here is what I have so far
This ends up with a nice little table that shows the id, my mileage at the time of fill up, the amount of the fill up, the amount of gallons purchased and the date of purchase. I have 5 months of data in my table. How do I write it so that the $row['miles'] can be subtracted by the next entry? It's actually kind of backwards because as it is, it would give me a negative number if I did the math. My attempt ended up giving me all 0's. Here is what the first 3 rows look like in my table
ID MILES AMOUNT GALLONS DATE
90 146547 $31.70 10.432 2015/07/25
103 147117 $28.42 10.226 2015/08/12
100 147419 $27.80 10.186 2015/08/22
so 146547 needs to be subtracted from 147117 and 147117 needs to be subtracted from 147419 and so on..
Code: Select all
while($row = mysqli_fetch_assoc($result)){
$date = str_replace('-','/', $row['date']);
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['miles'] . '</td><td>' . '$' . number_format($row['amount'], 2) . '</td><td> ' . $row['gallons'] . '</td><td> ' . $date . "</td></tr>";
}
ID MILES AMOUNT GALLONS DATE
90 146547 $31.70 10.432 2015/07/25
103 147117 $28.42 10.226 2015/08/12
100 147419 $27.80 10.186 2015/08/22
so 146547 needs to be subtracted from 147117 and 147117 needs to be subtracted from 147419 and so on..