Forum Moderators: coopster

Message Too Old, No Replies

Total of columns and subtracting fields form each other

Trying to get totals of fields and maths functions working

         

JSoaper

9:31 pm on Feb 13, 2009 (gmt 0)

10+ Year Member



Thanks to all who have helped so far in my first script. I can now add records and amend them.

I am using the script below to try achieve the following:
1. I want to total the amount column
2. I want to subtract distance2 form distance1 to arrive at the distance travelled
3. I need to have decimals automatically placed in the fields as well

All efforts to date have produced weird and wonderful results. As usual any help greatly appreciated.

===============================================
<table border="0" width="800" id="table1" style="font-family:arial" style="font-size:8pt"
div align="center">';

echo '<tr>';
echo '<th bgcolor="#C0C0C0" style="text-align:left;">Registration</th>';
echo '<th bgcolor="#C0C0C0" style="text-align:left;">Distance1</th>';
echo '<th bgcolor="#C0C0C0" style="text-align:left;">Distance2</th>';
echo '<th bgcolor="#C0C0C0" style="text-align:left;">Oil</th>';
echo '<th bgcolor="#C0C0C0" style="text-align:left;">Amount</th>';;
echo '</tr>';

$sql ="select * from `petrol` where $method like '%$text%'";
$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)){
echo "
<tr>
<td>".$row['reg']."</td>
<td>".$row['distance1']."</td>
<td>".$row['distance2']."</td>
<td>".$row['oil']."</td>
<td>".$row['amount']."</td>
<td><a href='edit_news.php?id=".$row['id']."'>Edit</a></td>

</tr>";

LifeinAsia

9:57 pm on Feb 13, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The tasks you're describing seem fairly straightforward. I'm not a PHP expert, but I'll try to show the concepts in pseudo code.
1. I want to total the amount column

Keep a running total as you loop through the query results:
set TotalAmount=TotalAmount + .$row['amount'].

I want to subtract distance2 form distance1 to arrive at the distance travelled

A simple subtraction should do the trick:
set TotalDistance = .$row['distance2']. - .$row['distance1'].

I need to have decimals automatically placed in the fields as well

That should just be a matter of formatting the numbers with PHP.

Another alternative would be to do the math within the SQL query.

optik

2:43 am on Feb 14, 2009 (gmt 0)

10+ Year Member



You don't have to echo every line by the way, just the beginning and end of the html will do.

JSoaper

1:01 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



Sadly still not getting it right

trigoon

1:25 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



Something like this might help for the total amount.

$tamount = 0;
$sql ="select * from `petrol` where $method like '%$text%'";
$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)){
$tamount += $row['amount'];

echo "
<tr>
<td>".$row['reg']."</td>
<td>".$row['distance1']."</td>
<td>".$row['distance2']."</td>
<td>".$row['oil']."</td>
<td>".$row['amount']."</td>
<td><a href='edit_news.php?id=".$row['id']."'>Edit</a></td>

</tr>";

As for the last two, can you clarify a bit more? Like for distance traveled will you want that for each row or for the total at the end? For number format can you give some examples? Do you just want an arbitrary 2 decimals added to the end or some pattern?