Solved

How to total added loops

Posted on 2013-05-14
7
164 Views
Last Modified: 2013-05-14
I have a small sum that loops for each individual item. Is there a way to total the subtotals though?

<?php 
$theqty = $row_Recordset1['qtyy'];
$theprice = $row_Recordset1['qprice'];
$thetotal = $theqty * $theprice;
echo $thetotal; 
?>

Open in new window

0
Comment
Question by:jonofat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39164300
There is no loop shown in the code sample, so there would not be any subtotals.
0
 

Author Comment

by:jonofat
ID: 39164310
Sorry..

 <?php $i=0; do { ?>
          <tr>
            <td class="bottomborder"> <input name="record[<?php echo $i ?>][qtyy]" value="<?php echo $row_Recordset1['qtyy']; ?>" size="1" />
              x <?php echo $row_Recordset1['pdescription']; ?>
              <input name="record[<?php echo $i ?>][productidd]" type="hidden" id="record[<?php echo $i ?>][productidd]" value="<?php echo $row_Recordset1['productidd']; ?>" /></td>
            <td class="bottomleft">R <?php echo $row_Recordset1['qprice']; ?> x <?php echo $row_Recordset1['qtyy']; ?>= <span class="bottomborder">
              R<?php $theqty = $row_Recordset1['qtyy'];
$theprice = $row_Recordset1['qprice'];
$thetotal = $theqty * $theprice;
echo $thetotal; ?>
              ex. vat
              <input name="totalprice" type="hidden" id="totalprice" value="<?php echo $thetotal; ?>" />
              </span></td>
          </tr>
          <?php $i++; } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39164344
How would the subtotals be grouped (what column name)?
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:jonofat
ID: 39164363
Well, it wouldn't be a column name I would use, wouldn't it be $thetotal ?

$thetotal = $theqty * $theprice;

which gives a subtotal for each line. But I need to add the subtotal of each line together. So if there are 3 individual products that look like this:

2 x $4 = $8
1 x $3 = $3
3 x $4 = $12

I somehow need it to add those up to give me $23.

I don't know if it would be some kind of foreach loop with $thetotal or what?
0
 

Author Comment

by:jonofat
ID: 39164386
I took a wild guess with this but it didn't get me anywhere.

$sum = 0;
foreach($thetotal as $value) $sum = $sum + $value;
echo $sum;
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39164436
OK, I think I get it better now.  This may not be exact (I can't test it), but it should be pretty close.
<?php 

// INITIALIZE A VARIABLE
$sum_total = 0;
 
$i=0; 

do { ?>
          <tr>
            <td class="bottomborder"> <input name="record[<?php echo $i ?>][qtyy]" value="<?php echo $row_Recordset1['qtyy']; ?>" size="1" />
              x <?php echo $row_Recordset1['pdescription']; ?>
              <input name="record[<?php echo $i ?>][productidd]" type="hidden" id="record[<?php echo $i ?>][productidd]" value="<?php echo $row_Recordset1['productidd']; ?>" /></td>
            <td class="bottomleft">R <?php echo $row_Recordset1['qprice']; ?> x <?php echo $row_Recordset1['qtyy']; ?>= <span class="bottomborder">
              R<?php $theqty = $row_Recordset1['qtyy'];
$theprice = $row_Recordset1['qprice'];
$thetotal = $theqty * $theprice;
echo $thetotal; 

// ADD TO THE VARIABLE
$sum_total = $sum_total + $thetotal;

?>
              ex. vat
              <input name="totalprice" type="hidden" id="totalprice" value="<?php echo $thetotal; ?>" />
              </span></td>
          </tr>
<?php 

$i++; 
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); 

// AT END OF LOOP, SHOW THE VARIABLE
var_dump($sum_total);

?>

Open in new window

0
 

Author Comment

by:jonofat
ID: 39164456
Tested it. Works perfectly. Genius! :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question