Forum Moderators: coopster

Message Too Old, No Replies

Categories

         

sebflipper

2:01 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



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.

lobo235

2:36 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



You could use tables or you could use <pre> tags to format it so there were the correct number of spaces.

sebflipper

3:04 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



I have tried putting them into tables.

But I cannot seems to get it to add the right number of: "<td>&nbsp;</td>" To each sub-category. It either adds one to many or one less than what's needed, and I can't seem to work out why.

coopster

11:58 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The only way I can think of would be to use some form of counting technique. Increment the counter by one every time you are indenting, decrement it or reset it when you are moving on to the next.

mincklerstraat

1:19 pm on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and echo str_repeat('-', $n)