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
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
Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.