Reply
Performing Calculations in PHP
Old 11-23-2007, 07:50 PM Performing Calculations in PHP
Harlequin's Avatar
Super Talker

Posts: 148
Name: Mick
Location: Tenerife
Hi guys, I've got this far with a bit of code:

HTML Code:
<td><?php echo $Row03['QtyOrdered']; ?></td>
   <td><?php echo $Row03['Brand']; ?> <?php echo $Row03['ItemDescription']; ?></td>
   <td><?php echo $Row03['Price']; ?></td>
   <td><?php echo number_format ($Row03['QtyOrdered'] * $Row03['Price'],2); ?></td>
So, the user might see something like this:

Qty: Item: Cost: Totals:
2 Nescafe Classic 50g 2.50 5.00
1 McVities Hob Nobs 250g 2.33 2.33


Now, the above is embedded within a while loop pulling orders from a database but it only occured to me today, hey - what if the client select 2 of this and 1 of that. I need to total up these items.

So, as you can see, I can total up the rows, but I'm stuck on how I can total up the column "Totals"

Any help on this would be appreciated because I think it's my final stumbling block.
Harlequin is offline
Reply With Quote
View Public Profile Visit Harlequin's homepage!
 
When You Register, These Ads Go Away!
Old 11-24-2007, 02:38 PM Re: Performing Calculations in PHP
Ultra Talker

Posts: 250
Hm,
I am not sure am I understand your question, but if you need total of last column (Totals) I think this script below will work for you if you insert in the while loop:

Quote:
$totaloftotals = @$totaloftotals + $Row03['Price'];
Shivaji
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Old 11-24-2007, 06:13 PM Re: Performing Calculations in PHP
Harlequin's Avatar
Super Talker

Posts: 148
Name: Mick
Location: Tenerife
Hi

Basically the last column does this:

SubTotal = Column 1 * Column 2

I then need to total all the SubTotals.

Do you think this will do it...?
Harlequin is offline
Reply With Quote
View Public Profile Visit Harlequin's homepage!
 
Old 11-25-2007, 06:30 AM Re: Performing Calculations in PHP
Harlequin's Avatar
Super Talker

Posts: 148
Name: Mick
Location: Tenerife
Can anyone else help with this at all...?
Harlequin is offline
Reply With Quote
View Public Profile Visit Harlequin's homepage!
 
Old 11-25-2007, 08:07 AM Re: Performing Calculations in PHP
Harlequin's Avatar
Super Talker

Posts: 148
Name: Mick
Location: Tenerife
<table>
<tr>
<th width="50">Qty:</th>
<th width="250">Item:</th>
<th width="25" align="center">Cost:</th>
<th width="25" align="center">Totals:</th>
</tr>
<?php
while($Row03 = mysql_fetch_array($Results03))
{
?>
<tr>
<td><?php echo $Row03['QtyOrdered']; ?></td>
<td><?php echo $Row03['Brand']; ?> <?php echo $Row03['ItemDescription']; ?></td>
<td align="right"><?php echo $Row03['Price']; ?></td>
<td align="right"><?php echo $SubTotal = number_format ($Row03['QtyOrdered'] * $Row03['Price'],2); ?></td>
</tr>
<?php
// Create Session Variable For Sum Total:
$SubTotals = array
(
$SubTotal,
);
foreach ($SubTotals as $SubTotal)
{
// Add Row Values Here For Totals:

***** this is where i'm stuck *****

}
}
?>
</table>
Harlequin is offline
Reply With Quote
View Public Profile Visit Harlequin's homepage!
 
Old 11-25-2007, 08:16 AM Re: Performing Calculations in PHP
Skilled Talker

Posts: 73
Name: Mattias Nordahl
Location: Sweden
I'm sure shivaji's method will do just fine. Every time you calculate the total cost of one product in the loop, add that total to a "total of totals".

PHP Code:
$totalOfTotals 0;
while(...) {
    
$total = [some calculations];
    
$totalOfTotals += $total;

lizciz is offline
Reply With Quote
View Public Profile
 
Old 11-26-2007, 04:07 AM Re: Performing Calculations in PHP
Harlequin's Avatar
Super Talker

Posts: 148
Name: Mick
Location: Tenerife
Hi guys

I've tried this:
HTML Code:
<table>
   <tr>
   <th>Qty:</th>
   <th>Item:</th>
   <th>Cost:</th>
   <th>Totals:</th>
   </tr>
<?php
   while($Row03 = mysql_fetch_array($Results03))
   {
?>
   <tr>
   <td><?php echo $Row03['QtyOrdered']; ?></td>
   <td><?php echo $Row03['Brand']; ?> <?php echo $Row03['ItemDescription']; ?></td>
   <td align="right"><?php echo $Row03['Price']; ?></td>
   <td align="right"><?php echo $SubTotal = number_format ($Row03['QtyOrdered'] * $Row03['Price'],2); ?></td>
   </tr>
<?php
// Calculate Session Total:
   $GrandTotal = $SubTotal = number_format ($Row03['QtyOrdered'] * $Row03['Price'],2);
   $_SESSION['Total'] += $GrandTotal;
   }
?>
</table>
<p>Your Cart Total: <b>£<?php echo number_format ($GrandTotal,2); ?></b></p>
But it displays this:


So you can see, it's simply displaying the last item.

It's infuriating, I've tried clearing the session variable on loading the page and calculating it fresh, I've tried embedding the code inside the while statement and if the page refreshes it screws up.

So close...

Yet so far...
Harlequin is offline
Reply With Quote
View Public Profile Visit Harlequin's homepage!
 
Old 11-26-2007, 07:09 AM Re: Performing Calculations in PHP
Ultra Talker

Posts: 250
Code:
<p>Your Cart Total: <b>£<?php echo number_format ($GrandTotal,2); ?></b></p>
I think how is here your mistake. Try to echo here:

Code:
<p>Your Cart Total: <b>£<?php echo $_SESSION['Total']; ?></b></p>
Shivaji
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Old 11-26-2007, 01:36 PM Re: Performing Calculations in PHP
Skilled Talker

Posts: 73
Name: Mattias Nordahl
Location: Sweden
The way you wrote your code you don't need your $SubTotal variable any more, since you save the same value in $GrandTotal. In other words, you replaced $SubTotal with $GrandTotal, without removing $SubTotal.

Instead, remove the $GrandTotal variable, because in your case the session variable ($_SESSION['Total']) is your grand total, or "total of totals".
lizciz is offline
Reply With Quote
View Public Profile
 
Old 12-04-2007, 09:37 AM Re: Performing Calculations in PHP
Harlequin's Avatar
Super Talker

Posts: 148
Name: Mick
Location: Tenerife
Thanks for all your help guys, site's now live - just waiting to be paid
__________________
Amazing Web Design ~ Website Design Tenerife
Death Once Had a Near Harlequin Experience...!
Are You An Ethical Coder...?
Harlequin is offline
Reply With Quote
View Public Profile Visit Harlequin's homepage!
 
Reply     « Reply to Performing Calculations in PHP
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.18064 seconds with 12 queries