Jump to content

[SOLVED] Unlimited Category and Subcategory


drisate

Recommended Posts

Hey guys i need to make a directory kind script but i have no idea how to make the script handdle unlimited categorys ... Ex

 

The problem i have is i don't get how to loop the listing when you have no idea how much loops you have to make...

The way i was thinking of making the table is

 

id (unique and auto increment

s_id (the unique id of the parent category)

name (the name of the category)

 

if s_id is equal to 0 it's a primary cat and for the rest it means they belog to an other one.

 

1

|__2

|__3

|__4

    |__5

          |__6

    |__7

8

|__9

 

in this case i loop s_id = to 0 and it returns 1 and 8

Now in the 1 loop i have to loop everything using 1 as s_id and that returns 2 3 and 4

 

My script is going nowhere because it's limited to the number of times i check ... i need it to be able to have unlimited subcategorys and be able to list them with out any problems what ever number there is. Any idea, script exemple or what ever that could help me?

Link to comment
Share on other sites

What you're going to want to do is:

 

1--Grab all categories that don't have a parent (parent_id = 0 or whatnot...your 1 and 8)

 

2--Grab the children of the first root category...and for each child, check if they have children...and for each child...check if they have children...etc.

 

You can do so with 2 functions...with the second calling itself repeatedly.  I've got a rather dirty version in a MySQL class I've written for PHP 5 that I'll post below to help you out:

 


// categorize(id, parent_id, category_name, prefix_to_add_each_next_level)

    public function categorize($parent, $child, $name= '', $pre='     ')
    {
        $cols = $this->getnumcolumns();
        while($row = $this->getrow())
        {
            $rawkeys = array_keys($row);
            $y = count($rawkeys);
            for($x = 1; $x < $y; $x+=2) $keys[] = $rawkeys[$x];
            for($x = 0; $x < $cols; $x++) $data[$keys[$x]][] = $row[$x];
        }
        if($name != '' && !array_key_exists($name, $data)) $name = '';
        $root = array_keys($data[$child], 0);
        $y = count($root);
        for($x = 0; $x < $y; $x++)
        {
            $prefix = '';
            for($z = 0; $z < $cols; $z++) $temp[$keys[$z]] = $data[$keys[$z]][$root[$x]];
            $this->categorized[] = $temp;
            $this->getchild($cols, $parent, $child, $keys, $data, $data[$parent][$root[$x]], $name, $prefix, $pre);
        }
        return $this->categorized;
    }
       
    private function getchild($cols, $parent, $child, $keys, $data, $value, $name, $prefix, $pre)
    {
        $prefix .= $pre;
        $children = array_keys($data[$child], $value);
        $x = count($children);
        if($x >= 1)
        {
            for($y = 0; $y < $x; $y++)
            {
                if($name != '') $data[$name][$children[$y]] = $prefix.$data[$name][$children[$y]];
                for($z = 0; $z < $cols; $z++) $temp[$keys[$z]] = $data[$keys[$z]][$children[$y]];
                $this->categorized[] = $temp;
                $this->getchild($cols, $child, $parent, $keys, $data, $temp[$parent], $name, $prefix, $pre);
            }
        }
    }

Link to comment
Share on other sites

Thanks using you logic it gave me what i needed to find this

 

function catOptions2($parent2, $level2=0) {

    $sql2 = "SELECT cat_id, cat_name FROM categories WHERE parent_cat_id = '$parent2' ORDER BY cat_name";

    $res2 = mysql_query($sql2) or die(mysql_error());
    $str2 = '';
    while (list($id2, $name2) = mysql_fetch_row($res2)) {if ($level2!="0"){$level3=$level2-1;}else{$level3=$level2;}
        $indent2 = str_repeat("<img height='20' alt='' src='image/vide.gif' width='20'>", $level3);
        if ($level2!="0"){$this="<img alt='' src='image/arbre.gif'>";}
        
if ($level2%2 == 1){$bk="bgcolor='#DEDFDF'";}
        
$str2 .= "<table border='0' cellpadding='0' cellspacing='0' width='100%'>
<tr>
	<td width='80%' $bk><font size='1' face='Verdana'>$indent2$this<img alt='$id2' src='image/categ_logo.gif'> $name2</font></td>
	<td width='20%' $bk> </td>
</tr>
</table>\n";	

           // now call itself to get subcats of this cat
          $str2 .= catOptions2($id2, $level2+1);
          unset($this);unset($bk);
    }
    return $str2;
}

 

It's brilliant hehe it calls him self for subcategorys

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.