Skip to main content
October 18, 2006
Answered

Totalling and repeat region

  • October 18, 2006
  • 4 replies
  • 352 views
Hi Can Anybody Help,

I am using PHP with a mySQL database. One of my pages display results from the database in a table with repeat region on it. Is there a way of totalling fields on the page. for example field 1 25
field 2 30
field 3 5
total 60
Thanks inadvance
This topic has been closed for replies.
Correct answer Newsgroup_User
mt006 wrote:
> I am using PHP with a mySQL database. One of my pages display results from
> the database in a table with repeat region on it. Is there a way of totalling
> fields on the page.

Yes, it's quite easy. Create a variable to hold the total and add the
value of each field. Let's say your repeat region looks like this:

<?php do { ?>
<tr>
<td>Amount</td>
<td><?php echo $row_recordsetName['amount']; ?></td>
</tr>
<?php } while ($row_recordsetName = mysql_fetch_assoc($recordsetName)); ?>

Change it like this:

<?php $total = 0;
<?php do { ?>
<tr>
<td>Amount</td>
<td><?php echo $row_recordsetName['amount'];
$total += $row_recordsetName['amount'];?></td>
</tr>
<?php } while ($row_recordsetName = mysql_fetch_assoc($recordsetName)); ?>
<tr>
<td>Total</td>
<td><?php echo $total; ?></td>
</tr>


--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/

4 replies

Inspiring
October 18, 2006
number_format($number,2,".",",");

will format 0 as 0.00 and 1234 as 1,234.00

http://www.php.net/manual/en/function.number-format.php

Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.


October 18, 2006
Thanks for that got it working, both answers were very useful.

One last question

How do I format the total to look like this 0.00 rather than 0
Newsgroup_UserCorrect answer
Inspiring
October 18, 2006
mt006 wrote:
> I am using PHP with a mySQL database. One of my pages display results from
> the database in a table with repeat region on it. Is there a way of totalling
> fields on the page.

Yes, it's quite easy. Create a variable to hold the total and add the
value of each field. Let's say your repeat region looks like this:

<?php do { ?>
<tr>
<td>Amount</td>
<td><?php echo $row_recordsetName['amount']; ?></td>
</tr>
<?php } while ($row_recordsetName = mysql_fetch_assoc($recordsetName)); ?>

Change it like this:

<?php $total = 0;
<?php do { ?>
<tr>
<td>Amount</td>
<td><?php echo $row_recordsetName['amount'];
$total += $row_recordsetName['amount'];?></td>
</tr>
<?php } while ($row_recordsetName = mysql_fetch_assoc($recordsetName)); ?>
<tr>
<td>Total</td>
<td><?php echo $total; ?></td>
</tr>


--
David Powers
Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
http://foundationphp.com/
Inspiring
October 18, 2006

Add each value to a running total variable as each one is output to the
page, then display that running total variable as the total at the end of
the page.

HTH
Cheers,
Rob