• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

[PHP] Categories

Question

sebflipper    0

I am trying to make a system that has unlimited categories and sub-categories.

So far in my database I have:

id, parent_id, cat_order, cat_name and visible.

And this is the little script I made to display them all:

function view_cat($cat_id)
{
    $select_sql = "SELECT * FROM `categories` WHERE `parent_id` =$cat_id";
    $resultlink = mysql_query($select_sql);
    if ($members = mysql_fetch_array($resultlink)) {
        do {                    
            echo " -> $members[cat_name]";
            view_cat($members[id]);
        } while ($members = mysql_fetch_array($resultlink));                    
    }
    else
    {
        echo "<br>";
    }                    
}
                
$select_sql = "SELECT * FROM `categories` WHERE `parent_id` =0";
$resultlink = mysql_query($select_sql);
if ($members = mysql_fetch_array($resultlink)) {
    do {                    
        echo "$members[cat_name]";
        view_cat($members[id]);
    } while ($members = mysql_fetch_array($resultlink));                    
}
else
{
    echo "<center><b>No Categories found</b></center>";
}        

This outputs the categories like:

Cat 1,0 -> Cat 1,1 -> Cat 1,2

Cat 2,0 -> Cat 2,1

But I cannot seem to get sub-categories working right, I want to output like this:

Cat 2,0 -> Cat 2,1 -> Cat 2,2

            -> Cat 2,1

But Currently it only outputs like this:

Cat 2,0 -> Cat 2,1 -> Cat 2,2

-> Cat 2,1

Any ideas how I can get it display under the category but with the correct indent? Thanks for any help, my heads going :wacko: trying to figure it out!

Edited by sebflipper

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.