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

  • SEARCH
  • Home
  • 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 7731913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:35:59+00:00 2026-06-01T06:35:59+00:00

$sth = $db->query(SELECT * FROM categories WHERE parent = 0); $sth->setFetchMode(PDO::FETCH_ASSOC); while ($row =

  • 0
$sth = $db->query("SELECT * FROM categories WHERE parent = 0");

$sth->setFetchMode(PDO::FETCH_ASSOC);

while ($row = $sth->fetch()) {

   echo $row['title'] . '<br />';

   $sth2 = $db->prepare("SELECT * FROM categories WHERE parent = ?");
   $sth2->bindParam(1, $row['id']);
   $sth2->execute();

   while ($row2 = $sth2->fetch()) {
        echo '..' . $row2['title'] . '<br />';

        $sth3 = $db->prepare("SELECT * FROM categories WHERE parent = ?");
        $sth3->bindParam(1, $row2['id']);
        $sth3->execute();

        while ($row3 = $sth3->fetch()) {
            echo '....' . $row3['title'] . '<br />';
        }
   }


}

As you can see I have ‘hard coded’ every while loop for mainly a “main category”, ‘a subcategory’ and finally it’s subcategory.
However I feel that I could change this, to automatically, loop the subcategories in the subcategories, if there are such.

The structure is simple of my SQL:
id
parent
title
description

I am clueless how I should solve this little problem… A while loop over a if-statement or something? Help me out.

  • 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-01T06:36:01+00:00Added an answer on June 1, 2026 at 6:36 am

    What you can do if the number of categories is not too big, is get all categories in one go from the database and then build a multi-dimensional array in php.

    See this question and the answers for more details.

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

Sidebar

Related Questions

I want to select a row from mysql which matches a specific id. I
I have the following query: $query = $this->getEntityManager()->createQuery(' SELECT u, p, m FROM MyCoreBundle:User
Ive got query like this: select distinct UserID, Count(UserId) as MyNumber from dbo.User2User group
I'm copying ListViewItems from one ListView to another, sth. like: foreach (ListViewItem item in
I have a row of data which contains numbers and splits by '-', sth
public ActionResult ReadXMLDevices(int groupID) { var query = from k in XMLEntities.unassigneditems where k.DevOrAcc
I want to do sth. like this: foo=(a b c) foo-=b echo $foo #
This question might be lazy question. While I was using netbean to run query
I have dynamicly generated SELECT. I try to return result as SETOF RECORD. Sth
I have a where clause in my query like this WHERE sth REGEXP '[0-9]{5,10}'

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.