Results 1 to 5 of 5

Thread: Group Records in While Loop

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Location
    San Isidro
    Posts
    326

    Group Records in While Loop

    Can you help me please..

    I have an sql query where the result is like this

    Code:
    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:
    while ($array = mysql_fetch_array($query)) {
    
    
    }
    I want to learn more
    grace

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Group Records in While Loop

    I think, this query will help you:
    Code:
    SELECT project_name, SUM(amount) AS total FROM tblProjects GROUP BY project_name
    The result should be like this:
    Code:
    project_name     total
    Project1         300
    Project2         500

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Location
    San Isidro
    Posts
    326

    Re: Group Records in While Loop

    I want to group it in my while loop instead of the query
    I want to learn more
    grace

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Group Records in While Loop

    Why?

    You should use SQL for what it's designed to do.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Location
    San Isidro
    Posts
    326

    Re: Group Records in While Loop

    Quote Originally Posted by penagate View Post
    Why?

    You should use SQL for what it's designed to do.
    because if i use the sql statement

    "SELECT project_name, SUM(amount) AS total FROM tblProjects GROUP BY project_name"

    it will group all project name

    my purpose is to group the projectname and the nature of the project and display it in an html report that look like the attached.
    Attached Images Attached Images  
    I want to learn more
    grace

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width