Loop and IF Statement, or grouping.... don't know

ms msreyes 08 Jun, 2022
What I am trying to do is a loop for a PDF. That works fine (I understand that syntax).
​
What I am struggling with is groupings. In my case my table fields consists of "Category" and "Items". I only want to output the "Category" once, or when it changes. For example:
​
(TABLE DATA EXAMPLE)
CATEGORY: Pants
ITEM: Straight Leg
​
CATEGORY: Pants
ITEM: Long Leg
​
CATEGORY: Shirt
ITEM: Short Sleeve
​
CATEGORY: Shirt
ITEM: Long Sleeve
​
ETC...
​
​
What I am looking is for output to be something like:
​
(OUTPUT EXAMPLE)
CATEGORY: Pants
ITEM: Straight Leg
ITEM: Long Leg
​
CATEGORY: Shirt
ITEM: Short Sleeve
ITEM: Long Sleeve
Gr GreyHead 09 Jun, 2022
HI msreyes,
​
Please turn on the Debugger output and copy and paste the relevant part here so we can see what the structure of the data is like.
​
Bob
ms msreyes 20 Jun, 2022
If you look at [selected_compartment] you will see when it changes....
​
​
Array
(
[DISPLAY_validate_fields] => Array
(
[log] => Automatic validation enabled.
[var] => 1
)

[read_data9] => Array
(
[log] => Array
(
[0] => SELECT `Data9`.`item` AS `Data9.item`, `Data9`.`choice` AS `Data9.choice`, `Data9`.`notes` AS `Data9.notes`, `Data9`.`select_truck` AS `Data9.select_truck`, `Data9`.`select_compartment` AS `Data9.select_compartment`, `Data9`.`firefighter_no` AS `Data9.firefighter_no` FROM `josej_chronoforms_data_truck-checklist-data` AS `Data9` WHERE `Data9`.`select_truck` = 'P1' AND DATE_FORMAT(`Data9`.`created` , "%Y-%m-%d") = '2022-05-30' AND `Data9`.`choice` = 'YES' ORDER BY `Data9.select_compartment` ASC LIMIT 1000;
)

[var] => Array
(
[0] => Array
(
[Data9] => Array
(
[item] => BOX CAUTION TAPE
[choice] => YES
[notes] =>
[select_truck] => P1
[select_compartment] => DRIVERS (1)
[firefighter_no] => 117
)

)

[1] => Array
(
[Data9] => Array
(
[item] => BOLT CUTTER
[choice] => YES
[notes] =>
[select_truck] => P1
[select_compartment] => DRIVERS (1)
[firefighter_no] => 117
)

)

[2] => Array
(
[Data9] => Array
(
[item] => 5LB SLEDGE HAMMER
[choice] => YES
[notes] =>
[select_truck] => P1
[select_compartment] => DRIVERS (1)
[firefighter_no] => 117
)

)

[3] => Array
(
[Data9] => Array
(
[item] => AXE
[choice] => YES
[notes] =>
[select_truck] => P1
[select_compartment] => DRIVERS (1)
[firefighter_no] => 117
)

)

[4] => Array
(
[Data9] => Array
(
[item] => HALLIGAN
[choice] => YES
[notes] =>
[select_truck] => P1
[select_compartment] => DRIVERS (1)
[firefighter_no] => 117
)

)


[5] => Array
(
[Data9] => Array
(
[item] => HOSE WASHER
[choice] => YES
[notes] =>
[select_truck] => P1
[select_compartment] => DRIVERS (2)
[firefighter_no] => 117
)

)

[6] => Array
(
[Data9] => Array
(
[item] => (2) HOSE WRENCH
[choice] => YES
[notes] =>
[select_truck] => P1
[select_compartment] => DRIVERS (2)
[firefighter_no] => 117
)

)

[7] => Array
(
[Data9] => Array
(
[item] => 65mm NOZZLE
[choice] => YES
[notes] =>
[select_truck] => P1
[select_compartment] => DRIVERS (2)
[firefighter_no] => 117
)
Gr GreyHead 23 Jun, 2022
Hi msreyes,
​
It looks to me as if you need a second loop inside the first one. The second loop would check if the category has changed and if so start a new section.
​
It would be more complex if the changes were not sequential, then I think you would need to check the first entry, scan for matching categories, then move on to the next 'different' category.
​
Bob
This topic is locked and no more replies can be posted.