0

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
8
  • 1
    can you print_r($packages->item) instead Commented Oct 16, 2012 at 0:43
  • preformatted its around 1000 lines Commented Oct 16, 2012 at 0:47
  • Add it to paste bin .. want to be sure of what am dealing with here Commented Oct 16, 2012 at 0:51
  • there is a syntax error in your code, in your condition Commented Oct 16, 2012 at 0:53
  • Why are you opening and closing PHP tags on every line? Commented Oct 16, 2012 at 0:54

2 Answers 2

1

$subtype has no role to play since it was set to Null before the if statement

$subtype = null;
if($subtype != $type->name)  <------- This would always be true

Category Name is also duplicated since its in the inner loop instead of outer loop

This is all i think you need

printf("<h2>%s</h2>", $packages->id);
foreach ( $packages->item as $item ) {
    printf("<h3>%s</h3>", $type->name);
    print("<ul>");
    foreach ( $item->type as $type ) {
        printf("<li>%s</li>", $item->name);
    }
    print("</ul>");
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is still giving me the same result, check my comment above with my pastebin.
@dynamo let me work with your real objects then add serialize($packages) to past bin .. i would be able to work with your objects directly
Figured out my code does work correctly, just has to be a php block and not open/close each line. Going to accept because your code helped me figure it out.
@dynamo good to know .... but you don't have to write if else that way ... You need to make your code clean and readable
1

As a solution to your above mentioned problem please refer the below code snippet

    <h2>Package <?=$packages->id?></h2> 
   <?php foreach($packages->item as $item):?> 
    <h3><?php echo $item->name;?></h3>

   <?php foreach($item->type as $type):?>
   <?php $subtype = null;?> 
   <?php if($subtype != $type->name)?> 
    <?=$type->name?><br> 
   <?php $subtype = $type->name;?> 
  <?php endforeach;?> 
  <?php endforeach;?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.