Trying to loop thru a result that has items. Items have a type and trying to echo out the category title for each group. Normally this works excellent with one loop but the $item-type foreach I Think is throwing things off. Any solutions?
<h2>Package <?=$packages->id?></h2>
<?php foreach($packages->item as $item):?>
<?php foreach($item->type as $type):?>
<?php $subtype = null;?>
<?php if($subtype != $type->name)?>
<h3><?=$type->name?></h3>
<?=$item->name?><br>
<?php $subtype = $type->name;?>
<?php endforeach;?>
<?php endforeach;?>
DB structure:
items
id name
1 mainitem1
2 mainitem2
3 item1
4 item2
5 item3
6 item4
types
id name
1 category 1
2 category 2
3 subcategory1
4 subcategory2
item_type
id item_id type_id
1 1 1
2 2 2
3 3 3
4 4 3
5 5 4
6 6 4
packages
id item_id
1 1
2 2
item_package
id package_id item_id
1 1 3
2 1 5
3 2 4
4 2 6
What my result currently is:
package 1
category 1
item 3
category 1
item 5
category 2
item 4
category 2
item 6
Desired result:
package 1
category 1
item 3
item 5
category 2
item 4
item 6
$subtypehas no role to play since it was set to Null before the if statementCategory Name is also duplicated since its in the inner loop instead of outer loop
This is all i think you need