Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8673975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:35:28+00:00 2026-06-12T19:35:28+00:00

Trying to loop thru a result that has items. Items have a type and

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:35:30+00:00Added an answer on June 12, 2026 at 7:35 pm

    $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>");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m trying to loop thru an array of items and put each of them
I have a while loop that fetches and echos status updates. I am trying
I'm trying to loop through a number of items, and create a json object.
I am trying to loop though my users database to show each username in
I am trying to loop through a vectors elements and trying to find out
I'm trying to loop an animation of each object bouncing one after another and
I'm trying to loop over distinct values over a dictionary list: So I have
Trying to loop through an array of hashes and getting an error message that
I am trying loop on selected elements that queried with document.querySelectorAll, but how? For
Im trying to loop over some array, and through that , update with new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.