Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 164
Name: Mick
Location: Tenerife
Trades: 0
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!
 
 
Register now for full access!
Old 11-24-2007, 03:38 PM Re: Performing Calculations in PHP
shivaji's Avatar
Ultra Talker

Posts: 318
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE
- uncommon free scripts

Please login or register to view this content. Registration is FREE
- Städte, Sport, Party, Gourment, Apartments, Hotels
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Old 11-24-2007, 07:13 PM Re: Performing Calculations in PHP
Harlequin's Avatar
Extreme Talker

Posts: 164
Name: Mick
Location: Tenerife
Trades: 0
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, 07:30 AM Re: Performing Calculations in PHP
Harlequin's Avatar
Extreme Talker

Posts: 164
Name: Mick
Location: Tenerife
Trades: 0
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, 09:07 AM Re: Performing Calculations in PHP
Harlequin's Avatar
Extreme Talker

Posts: 164
Name: Mick
Location: Tenerife
Trades: 0
<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, 09:16 AM Re: Performing Calculations in PHP
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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 Visit lizciz's homepage!
 
Old 11-26-2007, 05:07 AM Re: Performing Calculations in PHP
Harlequin's Avatar
Extreme Talker

Posts: 164
Name: Mick
Location: Tenerife
Trades: 0
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, 08:09 AM Re: Performing Calculations in PHP
shivaji's Avatar
Ultra Talker

Posts: 318
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE
- uncommon free scripts

Please login or register to view this content. Registration is FREE
- Städte, Sport, Party, Gourment, Apartments, Hotels
shivaji is offline
Reply With Quote
View Public Profile Visit shivaji's homepage!
 
Old 11-26-2007, 02:36 PM Re: Performing Calculations in PHP
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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 Visit lizciz's homepage!
 
Old 12-04-2007, 10:37 AM Re: Performing Calculations in PHP
Harlequin's Avatar
Extreme Talker

Posts: 164
Name: Mick
Location: Tenerife
Trades: 0
Thanks for all your help guys, site's now live - just waiting to be paid
__________________

Please login or register to view this content. Registration is FREE

Death Once Had a Near Harlequin Experience...!
Harlequin is offline
Reply With Quote
View Public Profile Visit Harlequin's homepage!
 
Reply     « Reply to Performing Calculations in PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.32524 seconds with 12 queries