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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:17:21+00:00 2026-05-26T19:17:21+00:00

I have a SQL table tbl_categories with these fields: id , parent , title

  • 0

I have a SQL table tbl_categories with these fields:

id , parent , title

for instance, the table may contain this information:

id   parent  title
1     0      the main item
2     1      first sub item
3     1      second sub item
4     2      first sub sub item 
5     3      second sub sub item

for example: 1 is the top category, 2 and 3 are children of 1, 4 is child of 2 and 5 is child of 3.

I want to list this information like a tree structure using PHP, like this:

- 1. the main item
 -- 2.first sub item
  ---4.first sub sub item
 -- 3. second sub item
  ---5.second sub sub item

and consider to add the “-“‘s according to the level of the item in the tree.

So the question is: what is an appropriate algorithm for this task?

  • 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-26T19:17:22+00:00Added an answer on May 26, 2026 at 7:17 pm

    I’m assuming you use MySQL:

    <?php
    // connect to the database
    $dbh = new PDO("mysql:host=127.0.0.1;port=3306;dbname=test", "root", "");
    
    // prepare a statement that we will reuse
    $sth = $dbh->prepare("SELECT * FROM tbl_categories WHERE parent = ?");
    
    // this function will recursively print all children of a given row
    // $level marks how much indentation to use
    function print_children_of_id( $id, $level ) {
        global $sth;
        // execute the prepared statement with the given $id and fetch all rows
        $sth->execute(array($id));
        $rows = $sth->fetchAll();
    
        foreach($rows as $row)
        {
            // print the leading indentation
            echo str_repeat(" ", $level) . str_repeat("-", $level) . " ";
            // print the title, making sure we escape special characters
            echo htmlspecialchars($row['title']) . "\n";
            // recursively print all the children
            print_children_of_id($row['id'], $level+1);
        }
    }
    
    // now print the root node and all its children
    echo "<pre>";
    print_children_of_id( 0, 1 );
    echo "</pre>";
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL table which has a number of fields ID | Value
I have an SQL table like this : sales(product,timestamp) I want to display a
If i have a sql table with column like this id version subversion 1
Lets say I have this SQL statements: ALTER TABLE dbo.[tbl] ALTER COLUMN col1 varchar(300)
I have a table tbl_1 with fields fl_1 (varchar2 type), contain Hexadecimal format. Now
I have a SQL table with news stories and Unix timestamps. I'd like to
I have a SQL table like so: Update: I'm changing the example table as
I have a sql table which have the following data, Id City Country ---
I have an SQL table written in MSSQL: create table [action] ( action_id bigint
I have a SQL table where in each row I store the country and

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.