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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:15:56+00:00 2026-05-15T13:15:56+00:00

I am using this category script: <?php include(connect.php); $nav_query = mysql_query(SELECT * FROM `categories`

  • 0

I am using this category script:

<?php

include("connect.php");

$nav_query = mysql_query("SELECT * FROM `categories` ORDER BY `id`");
$tree = "";
$depth = 1;
$top_level_on = 1;
$exclude = array();
array_push($exclude, 0);

while ($nav_row = mysql_fetch_array($nav_query)) {
    $goOn = 1;
    for ($x = 0; $x < count($exclude); $x++) {
        if ($exclude[$x] == $nav_row['id']) {
            $goOn = 0;
            break;
        }
    }
    if ($goOn == 1) {
        $tree .= $nav_row['name'] . "<br>";
        array_push($exclude, $nav_row['id']);
        if ($nav_row['id'] < 6) {
                    $top_level_on = $nav_row['id'];
                }

        $tree .= build_child($nav_row['id']);
    }
}

function build_child($oldID) {
    global $exclude, $depth;
    $child_query = mysql_query("SELECT * FROM `categories` WHERE parent_id=" . $oldID);
    while ($child = mysql_fetch_array($child_query)) {
        if ($child['id'] != $child['parent_id']) {
            for ($c=0; $c < $depth; $c++) {
                            $tempTree .= "&nbsp;";
                        }
            $tempTree .= "- " . $child['name'] . "<br>";
            $depth++;
            $tempTree .= build_child($child['id']);
            $depth--;
            array_push($exclude, $child['id']);
        }
    }

    return $tempTree;
}

echo $tree;

?>

It relies on the following mysql database structure:

id | parent_id | name

1                Cats
2    1           Siamese Cats
3    2           Lilac Point Siamese Cats
4                Dogs

etc...

The script allows for unlimited category depth but has one major downfall. It displays the category navigation to the front end like so:

Cats
 - Siamese Cats
 - Lilac Point Siamese Cats
Dogs

How can I have it display like this:

Cats
 - Siamese Cats
   - Lilac Point Siamese Cats
Dogs

So that for each additional category depth another space with be added to the beginning indentation of the category text?

  • 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-15T13:15:58+00:00Added an answer on May 15, 2026 at 1:15 pm

    As you are already keep track of the depth, make use of it. E.g.

    $indent = '';
    for($i = 0; $i < $depth; $i++) {
         $indent .= "&nbsp;";
    }
    $tempTree .= $indent . "- " . $child['name'] . "<br>";
    

    To make it look the way you want it you might have to initialize $depth with 0.


    Also note that executing SQL queries in a nested for loop is not the best approach. If possible, try to reduce the number of queries.

    You could for example use classes and, get all the entries at once and then build the tree structure with objects and arrays.

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

Sidebar

Ask A Question

Stats

  • Questions 493k
  • Answers 493k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use: select * from sys.procedures where name like… May 16, 2026 at 10:36 am
  • Editorial Team
    Editorial Team added an answer If you're sending emails out once a day, you probably… May 16, 2026 at 10:36 am
  • Editorial Team
    Editorial Team added an answer MaxMind, which offers a popular IP-to-location database published some statistics… May 16, 2026 at 10:36 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am using Zend Gdata to interface with the YouTube API from a PHP
Hey, i am currently creating a pagination script using php and jquery and for
I have tested the below script on a demo page which is not using
Working on a PHP script to essentially dump the results of a query into
Using jQuery's UI Tabs. This is my code. <div id=tabs> <ul> <li><a href=#tabs-1>Find a
Using jQuery UI's XML data parsed once autocomplete. I've checked the feed and it
I'm using uploadify on a page that allows a user to upload images as
I've been reading about XML, XSL, XPath, etc. I want to start this small
Thanks for all the great answers, this helps a lot! Hello, I need some
I have five different websites set up using Wordpress 3.0 Multisite. They each contain

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.