Jump to content

php Array groups - headers and footers


JBRTaylor

Recommended Posts

Hi

 

I have a two dimensional array and would like to output the results in groups depending upon one of the fields in the array. While i have managed this and have figured out how to create a header for each group, i would now like to create a footer (end of group) line where i could create a subtotal or similar.

 

my working code with the header is below but how do i add the footer?

$group = null;
for($i=0;$i<count($rowKitCharges);$i++){

		if($rowKitCharges[$i]['SubCategory'] != $group)
    {
         //echo the group header
         echo "<br><b>" . $rowKitCharges[$i]['SubCategory'] . "</b><br>";
				
				$group = $rowKitCharges[$i]['SubCategory'];
      }
    
		
		echo $rowKitCharges[$i]['HireID'] . " " . $rowKitCharges[$i]['SubCategory'];
                echo "<br>";
	
}

Thanks

Jon

Link to comment
https://forums.phpfreaks.com/topic/292804-php-array-groups-headers-and-footers/
Share on other sites

Like this

$rowKitCharges = [
                [
                'SubCategory' => 'A',
                'HireID'      => 123
                ],
                [
                'SubCategory' => 'A',
                'HireID'      => 124
                ],
                [
                'SubCategory' => 'B',
                'HireID'      => 125
                ],
                [
                'SubCategory' => 'C',
                'HireID'      => 126
                ],
                [
                'SubCategory' => 'C',
                'HireID'      => 127
                ],
                [
                'SubCategory' => 'C',
                'HireID'      => 128
                ]
            ];
$group = null;
$count = 0;
for($i=0;$i<count($rowKitCharges);$i++){

    if($rowKitCharges[$i]['SubCategory'] != $group)
    {
        if ($group) {
            echo "<b>TOTAL ITEMS: $count</b><br><br>";  // FOOTER
        }
        //echo the group header
        echo "<br><b>" . $rowKitCharges[$i]['SubCategory'] . "</b><br>";
        $group = $rowKitCharges[$i]['SubCategory'];
        $count = 0;
    }
        
    echo $rowKitCharges[$i]['HireID'] . " " . $rowKitCharges[$i]['SubCategory'];
    ++$count;
    echo "<br>";
    
}
// OUTPUT FINAL FOOTER
echo "<b>TOTAL ITEMS: $count</b><br><br>";  // FOOTER

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.