Jump to content

Output 2 groups


craygo

Recommended Posts

I have a few tables That i want to report on. I want to output them into 2 groups. This is for a packing slip.
Example I have an order. It is going to 2 different addresses and using multiple shipping methods. I want to group the report by shipping address(already done) but also want to group it buy shipping method under it. so it would look like this

Some street
  fedex P1
    detail 1
    detail 2
  fedex P2
    detail 3
    detail 4
Next Street
  UPS Overnight
    detail 3
    detail 4

here is my query which I think is fine. Just need it to output corectly to the page
[code]$reports = "SELECT orderdetails.DetailNum AS detnum, shipping.ShipName AS shipname, orderdetails.ShipID AS shipid,
            shipmethod.ShipMethod AS shipmeth, orderdetails.ShipMethID AS shipmid, orderdetails.OrderID AS ordid
            FROM orderdetails
            JOIN shipping ON orderdetails.ShipID = shipping.ShipID
            JOIN orders ON orderdetails.OrderID = orders.OrderID
            JOIN shipmethod ON orderdetails.ShipMethID = shipmethod.ShipMethID
            WHERE orderdetails.OrderID = ".$_GET['ordid']."
            GROUP BY shipping.ShipName, orderdetails.ShipMethID, orderdetails.DetailNum";[/code]

Thanks

Ray
Link to comment
https://forums.phpfreaks.com/topic/28654-output-2-groups/
Share on other sites

You want ORDER BY, not GROUP BY.

GROUP BY is used when you want aggregate totals, averages etc.

[code]
$reports = "SELECT orderdetails.DetailNum AS detnum,
            shipping.ShipName AS shipname,
            orderdetails.ShipID AS shipid,
            shipmethod.ShipMethod AS shipmeth,
            orderdetails.ShipMethID AS shipmid,
            orderdetails.OrderID AS ordid
            FROM orderdetails
            JOIN shipping ON orderdetails.ShipID = shipping.ShipID
            JOIN orders ON orderdetails.OrderID = orders.OrderID
            JOIN shipmethod ON orderdetails.ShipMethID = shipmethod.ShipMethID
            WHERE orderdetails.OrderID = ".$_GET['ordid']."
            ORDER BY shipping.ShipName, orderdetails.ShipMethID, orderdetails.DetailNum";
           
$res = mysql_query($reports) or die(mysql_error());

$prevShipname = '';
$prevShipmeth = '';
while (list($detnum, $shipname, $shipid, $shipmeth, $shipmid, $ordid) = mysql_fetch_row($res)) {
    // has shipname changed?
    // if so assume shipmeth also changes
    if ($prevShipname != $shipname) {
        echo "<h3>$shipname</h3>";
        echo "<h4>$shipmeth</h4>";
        $prevShipname = $shipname;
        $prevShipmeth = $shipmeth;
    }
    elseif ($prevShipmeth != $shipmeth) {
        echo "<h4>$shipmeth</h4>";
        $prevShipmeth = $shipmeth;   
    }
    echo "$ordid $detnum<br/>";
   
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28654-output-2-groups/#findComment-131177
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.