PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Sum of results
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Sum of results

Réponse
 
LinkBack Outils de la discussion
Vieux 30/01/2008, 06h58   #1
Dax Solomon Umaming
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sum of results

Hi;

I've tried Googling this out but failed to search for an answer to this, so
I'm posting to this list. I'd like to know if there's a way to get the sum
this results:

// Results
Individual Daily Consumption
//AccountNo : Consumption
4146121002: 1.42
4146111002: 0.29
4146113002: 1.38
4146110002: 0.33
4146112002: 0.00
4146118002: 9.96
== MORE ==

// Code that generated the results
while ($row6 = mysql_fetch_assoc($queryconsumerresults)) {
// Show Consumer AccountNo and their Consumption
echo $row6['AccountNo'] . ": " . sprintf("%1.2f", (((($row6['Reading'] +
$row6['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1'])) /
$noofdays) * $row6['Multiplier'])) . "<br />";
}

I've tried getting the sum() from the MySQL table, but Multiplier is eitherat
1 or 2 or 2.5 and the only way I can get an accurate total consumption is
getting the sum from the results.

Is there a way I can place this code on an array? I've tried using
$indcons = array( sprintf("%1.2f", (((($row6['Reading'] + $row6
['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1'])) /
$noofdays) * $row6['Multiplier']))) but I've obviously failed.

--
Dax Solomon Umaming
http://knightlust.com/
GPG: 0x715C3547

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQBHoCAI6Hw0ZHFcNUcRApgCAKDA4gNzm06FNWaJDazi9I VO16ckqgCgu8Ck
/N/+T7ATqBUGzKlkiY4ZN8I=
=shfI
-----END PGP SIGNATURE-----

  Réponse avec citation
Vieux 30/01/2008, 07h12   #2
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Sum of results

On Jan 30, 2008 1:58 AM, Dax Solomon Umaming <knightlust@gmail.com> wrote:
> I've tried Googling this out but failed to search for an answer to this, so
> I'm posting to this list. I'd like to know if there's a way to get the sum
> this results:


you can simply sum them as you print the report

$sum = 0.0;
while ($row6 = mysql_fetch_assoc($queryconsumerresults)) {
$curVal = ((($row6['Reading'] + $row6['KwHrAdjustment']) -
($row6['Reading1'] + $row6['KwHrAdjustment1'])) / $noofdays) *
$row6['Multiplier']));
// Show Consumer AccountNo and their Consumption
echo $row6['AccountNo'] . ": " . sprintf("%1.2f", ( $curVal . "<br />";
// add curVal to sum
$sum += $curVal;
}

> Is there a way I can place this code on an array? I've tried using
> $indcons = array( sprintf("%1.2f", (((($row6['Reading'] + $row6
> ['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1'])) /
> $noofdays) * $row6['Multiplier']))) but I've obviously failed.


array() creates an array, what youve done on this line is created
an array with one element, which will be the last value of $row6 from the while
loop that precedes it. since the last value returned from mysql_fetch_assoc()
is false (thats what terminates the while loop), im guessing $indcons contains
false as its only value.
if you want to place these values in an array as you iterate over them, add this
inside the while loop:
$indcons[] = $curVal;

-nathan
  Réponse avec citation
Vieux 30/01/2008, 23h40   #3
Richard Lynch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Sum of results



On Wed, January 30, 2008 12:58 am, Dax Solomon Umaming wrote:
> Hi;
>
> I've tried Googling this out but failed to search for an answer to
> this, so
> I'm posting to this list. I'd like to know if there's a way to get the
> sum
> this results:
>
> // Results
> Individual Daily Consumption
> //AccountNo : Consumption
> 4146121002: 1.42
> 4146111002: 0.29
> 4146113002: 1.38
> 4146110002: 0.33
> 4146112002: 0.00
> 4146118002: 9.96
> == MORE ==
>
> // Code that generated the results
> while ($row6 = mysql_fetch_assoc($queryconsumerresults)) {
> // Show Consumer AccountNo and their Consumption
> echo $row6['AccountNo'] . ": " . sprintf("%1.2f",
> (((($row6['Reading'] +
> $row6['KwHrAdjustment']) - ($row6['Reading1'] +
> $row6['KwHrAdjustment1'])) /
> $noofdays) * $row6['Multiplier'])) . "<br />";
> }
>
> I've tried getting the sum() from the MySQL table, but Multiplier is
> either at
> 1 or 2 or 2.5 and the only way I can get an accurate total consumption
> is
> getting the sum from the results.


You should be able to use:
sum( (Reading + KwHrAdjustment - Reading1 + KwHrAdjustment1) /
$noofdays * Multiplier) from MySQL in another query...

But if you are already iterating through the results anyway, it's
probably just as easy to do:

$sum = 0;
while ($row6 = mysql_fetch_assoc(...)){
$consumption = $row6['Reading'] + ...;
$sum += $consumption;
}
echo "Total: $sum<hr />\n";

> Is there a way I can place this code on an array? I've tried using
> $indcons = array( sprintf("%1.2f", (((($row6['Reading'] + $row6
> ['KwHrAdjustment']) - ($row6['Reading1'] + $row6['KwHrAdjustment1']))
> /
> $noofdays) * $row6['Multiplier']))) but I've obviously failed.
>
> --
> Dax Solomon Umaming
> http://knightlust.com/
> GPG: 0x715C3547
>



--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h53.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09455 seconds with 11 queries