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 8873833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:33:52+00:00 2026-06-14T18:33:52+00:00

I’m joining 4 tables and want to create a while loop that spits out

  • 0

I’m joining 4 tables and want to create a while loop that spits out results nested under different categories.

My Tables

categories
id | category_name

pages
id | page_name | category

page_content
id | page_id | image_id

images
id | thumb_path

My current SQL join

<?php $all_photos = mysql_query("
    SELECT * FROM categories JOIN pages ON pages.category = categories.id
    JOIN image_pages ON image_pages.page_id = pages.id
    JOIN images ON images.id = image_pages.image_id
");?>

The result I want from a while loop

I would like to get something like this….

Category 1
page 1
Image 1, image 2, image 3

page 2
Image 2, image 4

Category 2
page 3
image 1

page 4
image 1, image 2, image 3

Each image can fall under multiple pages and each page can fall under multiple categories.

at the moment I have 2 solutions, one which lists each category several times according to the the amount of pages inside them:

eg. category 1, page 1, image 1 – category 1, page 1, image 2 etc

One that uses a while loop inside another while loop inside another while loop, resulting in 3 sql queries.

<?php $all_categories = mysql_query("SELECT * FROM categories");?>

<?php 

    while($all_category = mysql_fetch_array($all_categories)) {

        ?>
        <h4><?=$all_category['category_name']?></h4>

        <?php $all_pages = mysql_query("SELECT * FROM pages WHERE category = " . $all_category['id'] . "");?>

        <?php 
            while($all_page = mysql_fetch_array($all_pages)) {
            ?>
                <p><?=$all_page['page_name']?></p>


                <?php $all_images = mysql_query("SELECT * FROM images JOIN image_pages ON image_pages.page_id  = " . $all_page['id'] . " AND image_pages.image_id = images.id");

                ?>

                <div class="admin-images-block clearfix">

                <?php

                while($all_image = mysql_fetch_array($all_images)) {
                ?>


                    <img src="<?=$all_image['thumb_path']?>" alt="<?=$all_image['title']?>"/>


                <?php
                }

                ?>
                </div>
                <?php
            }
    }

?>
  • 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-14T18:33:54+00:00Added an answer on June 14, 2026 at 6:33 pm

    I think second solution is better and straight forward.
    You can do this like:

    Update:

      <?php
    $all_cats=mysql_query("select * from categories");
    while($all_cat = mysql_fetch_array($all_cats)){
        //print your cat title 
        $check = mysql_query("select * from images i, pages p, page_content pc categories c where c.id = p.category and p.id = pc.page_id and pc.image_id=i.id");
    
        if(mysql_num_rows($check) > 0){
    
        $all_pages=mysql_query("select * from pages where category=".$all_cat['id']);
        while($all_page = mysql_fetch_array($all_pages)){
            //print your page
            echo "<p>".$all_page['page_name']."</p>";
            ?>
            <div class="admin-images-block clearfix">
                <?php
    
                $all_images=mysql_query("select * from images where id=".$all_page['image_id']);
                while($all_image = mysql_fetch_array($all_images)) {
                //print your img
                ?>
                    <img src="<?=$all_image['thumb_path']?>" alt="<?=$all_image['title']?>"/>
                <?php}?>
             </div>
        <?php
        }
    
        }
    }
    ?>
    

    Best of luck 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.