PHP/SQL GroupBy *and* sum total of Grouped Figure

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PHP/SQL GroupBy *and* sum total of Grouped Figure

Post by simonmlewis »

Yes I did. Spent a lot of time checking it, but it's a style of code I have used a lot over the years, and am unfamiliar with other code.

Problem with the response was they were giving me very useful answers.... and then not really an answer at all, which is a little counter-productive.

I would personally say "have you checked this...".... I would say "try this in the code at this stage...".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP/SQL GroupBy *and* sum total of Grouped Figure

Post by VladSun »

I usually refuse to give people "copy-paste" solutions. You should know why I do this ...

You didn't even read what I posted - I said "Put sum() in the SELECT clause" and you ... you put it in the GROUP BY clause ...
I'm willing to help, but you are not willing to use it... I'm not a debugger, you know ...
There are 10 types of people in this world, those who understand binary and those who don't
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PHP/SQL GroupBy *and* sum total of Grouped Figure

Post by simonmlewis »

Nope, you're moody.
Never mind.

I did try BOTH options actually, but neither worked for me.

Just need some help and guidance here. Sometimes someone needs the route to a destination, not just to be given a signpost and continue getting lost.

By the way, I have used maps!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PHP/SQL GroupBy *and* sum total of Grouped Figure

Post by simonmlewis »

An additional note: this code:

Code: Select all

$result = mysql_query ("
SELECT persondealing, estimate, SUM(total) AS amount_per_person FROM matterusersrights 
INNER JOIN matterdata ON matterdata.clientcode = matterusersrights.clientcode 
WHERE matterusersrights.userid = '" .$cookieid."' GROUP BY persondealing");
 
while ($row = mysql_fetch_assoc($result)) 
{
        echo "<br/>
<b> $row->persondealing</b><br/>
  $row->estimate<br/>";
echo $row['amount_per_person'];
}       
    mysql_free_result($result);
    mysql_close($sqlconn);
Produces ONLY the sum of 'total', and doesn't echo anything else. I suspect it could be down toe fetch_assoc, as if I change assoc to object, it shows the name and estimate, but breaks at the $row[]; point.

ie. assoc works for the sum of total using the [] ,method. object works for the persondealing and estimate fields. But neither works for both.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: PHP/SQL GroupBy *and* sum total of Grouped Figure

Post by Mark Baker »

Either

Code: Select all

 
while ($row = mysql_fetch_assoc($result)) {
   echo $row['persondealing'];
   echo $row['estimate'];
   echo $row['amount_per_person'];
}
 
or

Code: Select all

 
while ($row = mysql_fetch_object($result)) {
   echo $row->persondealing;
   echo $row->estimate;
   echo $row->amount_per_person;
}
 
but this isn't pick and mix
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: PHP/SQL GroupBy *and* sum total of Grouped Figure

Post by simonmlewis »

Fantastic - I think as you say it was pick'n'mix.... using two different styles of rendering the values.

Thank you very much for a descriptive response. :D I knew there was someone out there.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply