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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:35:17+00:00 2026-05-11T21:35:17+00:00

sorry if you’ll find this question stupid, but I really need help. Here’s some

  • 0

sorry if you’ll find this question stupid, but I really need help. Here’s some info about it.

Database structure


id | parent_id | level | name

1 | 0 | 1 | Home page

2 | 1 | 2 | Child of homepage

3 | 1 | 2 | Another child of homepage

4 | 2 | 3 | Sub child of page id 2

5 | 3 | 3 | Sub child of page id 3


Question


How do I make the SQL statement to group my rows by level and make this output.

Desired output


Home page

~~Child of homepage

~~~~Sub child of page id 2

~~Another child of homepage

~~~~Sub child of page id 3

I know how to do this with a recursion method, but I want to know if I can accomplish this with one SQL statement.

  • 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-05-11T21:35:17+00:00Added an answer on May 11, 2026 at 9:35 pm

    Well, you could order the data by parent_id so that it is sorted correctly:

    $menu = mysql_query("SELECT * FROM menu ORDER BY parent_id, level, name");
    

    Then, use the level to determine your indent:

    while ($row = mysql_fetch_object($menu)) {
      echo str_repeat('~~', $row->level).' '.$row->name.'<br />';
    }
    

    Say you want them in nested lists or some other element (rather than indented by a prefix such as ‘~~’):

    $level = 0;
    echo '<ul>';
    while ($row = mysql_fetch_object($menu)) {
      if ($row->level > $level) {
        echo '<ul>';
      } else if ($row->level < $level) {
        echo '</ul>';
      }
      echo '<li> '.$row->name.'</li>';
      $level = $row->level;
    }
    for ($i = $level; $i >= 0; $i--) {
      echo '</ul>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this sounds like a really stupid question, but I need to make
Sorry if this is a stupid question... I've developed an application that creates absolute
Sorry if this is a noob question, but All my code is error-free and
Sorry about this newbie question. Is there a way to execute multiple sentences at
Sorry, guys.I am quite new in mysql but I do need help from getting
Sorry if it was already asked before, but I really couldn't find a clear
Sorry if this is a simple question, but I've only recently started learn coding
Sorry for real stupid question. But it does not work either way. <html> <head>
Sorry for this not being a real question, but Sometime back i remember seeing
(Sorry if this is a really long question, it said to be specific) The

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.