Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Totalling and repeat region

Guest
Oct 18, 2006 Oct 18, 2006

Copy link to clipboard

Copied

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
TOPICS
Server side applications

Views

336
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 18, 2006 Oct 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...

Votes

Translate
LEGEND ,
Oct 18, 2006 Oct 18, 2006

Copy link to clipboard

Copied


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



Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 18, 2006 Oct 18, 2006

Copy link to clipboard

Copied

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/

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 18, 2006 Oct 18, 2006

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 18, 2006 Oct 18, 2006

Copy link to clipboard

Copied

LATEST
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.


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines