Grouping Data

Discussion in 'PHP' started by pepe_lepew1962, Jan 4, 2011.

  1. #1
    Hello, I am having problems with displaying grouped records from mysql and could desperately use help. In the example below, I want to display the Order Number ONCE and ALL the records associated with that order. Then carry on to the next record and so on. Here is a sample of what the output is suppose to be. Can anyone help me with this. The key being to not display the Order Number on every line.


    tblName1.OrderNumber tblName2.Name tblName2.Price

    12653 PBXL 396.52
    ABCD 426.28
    GEDL 385.20

    12654 ABCD 515.18
    FGHL 520.00

    12659 PBXL 612.35
    FGHL 619.20
    XTRL 420.39


    Thanks.
     
    pepe_lepew1962, Jan 4, 2011 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    You could use a loop with 2 query's

    in the first query select the order number, and in the second query select the items for the order number ex:

    
    <?php
    
    $query1 = "SELECT tblName1.OrderNumber From tblName1 Order By tblName1.OrderNumber DESC;";
    
    $rs = mysql_query($query1) or die(mysql_errer());
    
    while($row = mysql_fetch_array($rs,MYSQL_BOTH)){
    
    	echo $row['tblName1.OrderNumber']."<br>\n";
    
    	$query2 = "SELECT tblName2.* FROM tblName1,tblName2 WHERE tblName1.OrderNumber=".
    	$row['tblName1.OrderNumber']." Order By tblName2.Name ASC;"
    
    	$rs2 = mysql_execute($query2) or die(mysql_error());
    	
    	while($row2 = mysql_fetch_array($rs2,MYSQL_BOTH)){
    
    		echo $row2['tblName2.Name']." ".$row2['tblName2.Price']."<br>\n";
    
    	}
    
    	mysql_free_result($rs2);
    }
    mysql_free_result($rs);
    
    ?>
    
    Code (markup):
     
    shofstetter, Jan 6, 2011 IP