Group Records in While Loop

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
acellec
php-forum Active User
php-forum Active User
Posts: 26
Joined: Thu May 17, 2012 5:44 pm

Thu May 17, 2012 6:05 pm

Can you help me please..

I have an sql query where the result is like this

Code: Select all

Projet Name 	Nature 		Amount
Project1    	infra		100
Project1    	noninfra	200
Project2	infra		500
I want to group by project name in my while loop, display project name once if has more than 1
and sum the amount of all records with the same project name. How will i do that ?

Code: Select all

while ($array = mysql_fetch_array($query)) {


}
i want the report to look like the attached copy.
acellec
php-forum Active User
php-forum Active User
Posts: 26
Joined: Thu May 17, 2012 5:44 pm

Thu May 17, 2012 6:16 pm

Reports copy.jpg
Reports copy.jpg (38.54 KiB) Viewed 1409 times
User avatar
macek
php-forum GURU
php-forum GURU
Posts: 277
Joined: Wed Aug 25, 2010 10:42 am
Contact:

Thu May 17, 2012 9:41 pm

SELECT Project_Name, Nature, SUM(Amount) AS Amount FROM table GROUP BY Project_Name
acellec
php-forum Active User
php-forum Active User
Posts: 26
Joined: Thu May 17, 2012 5:44 pm

Sun May 20, 2012 4:28 pm

how will i display it in html? so that it will be like the report above (image attached)? where Project name will appear once, and the sum of project name with the same nature of project, and the sum of projects with the same project name?
acellec
php-forum Active User
php-forum Active User
Posts: 26
Joined: Thu May 17, 2012 5:44 pm

Mon May 21, 2012 4:54 am

macek wrote:SELECT Project_Name, Nature, SUM(Amount) AS Amount FROM table GROUP BY Project_Name
if i will do that, i will not be getting the sum for the nature of project.

I need to sum it in the while loop.

Sum all the projects with the same project name. Please help me it's been month that I am solviing this problem
Post Reply