Jump to content

Parent/Chile Category Help


forumnz

Recommended Posts

I found this code on this site from the code articles. I now understand how this works (kind of).

 

Basically I want to alter this script so that lets say I open it, it will have a 0 value and display all categories.

Then if I click on a sub cat it will have another value and display only a small number of cats etc.

 

This should be simple for most of you but I am still a newbiw (self confessed) when it comes to PHP.

 

Your time is much appreciated.

Thanks,

Sam.

 

<? error_reporting(0);?>
<?
class categories
{

    var $cats = array();
    var $subs = num;
    var $cat_map = array();

        function get_cats_(&$cats_result , $parent_id = 0 , $level = 1)
        {

            for ($i=0; $i<count($cats_result); $i++)
            {
                if($cats_result[$i]['parent'] == $parent_id)
                {
                    $cats_result[$i]['level'] = $level;
                    $this->cats[] = $cats_result[$i];
                    $this->get_cats_($cats_result , $cats_result[$i]['id'] , $level+1 , $type);
                }
            }

        }

    function get_cats(&$cats_result , $parent_id = 0 , $level = 1)
    {
            $this->cats = array();
        $this->tmp_cats = array();
            $this->get_cats_($cats_result,$parent_id, $level);
    }

    function count_subs($id , $cats_result)
    {
            global $db;
            $this->tmp_cats = array();
        $this->subs = NULL;

            $this->get_cats($cats_result , $id , 1);
        $this->subs = count($this->tmp_cats);
    }

    function cat_map_($id , $cats_result)
    {
        for ($i=0; $i<count($cats_result); $i++)
        {
                $cats_result_[$cats_result[$i]['id']] = $cats_result[$i];
        }

        while(list($a,$b) = @each($cats_result_))
        {
                if($cats_result_[$id]['parent'] > 0 && $cats_result_[$id]['parent'] == $cats_result_[$a]['id'])
                {
                                $this->cat_map[] = $cats_result_[$a];
                    if($cats_result_[$a]['parent'] > 0)
                    {
                        $this->cat_map_($cats_result_[$a]['id'] , $cats_result , $type);
                    }
                }
        }

    }

    function cat_map($id , $cats_result)
    {
            @$this->cat_map = array();
        @$this->tmp_cat_map = array();
        $this->cat_map_($id , $cats_result);
        $this->cat_map = @array_reverse($this->cat_map);
    }
}

$cats = new categories();

mysql_connect('localhost','aaa','aaa');
mysql_select_db('bbmembers');
//GET the categories
$result = mysql_query('select * from categories');

//put all the categories in an array
$mycats = array();
while($row = mysql_fetch_array($result))
{
$mycats[] = array('id'=>$row['id'],'title'=>$row['title'],'parent'=>$row['parent'],'level'=>0);
}

//Start working
$cats->get_cats($mycats);

//printing cats by level
for ($i=0; $i<count($mycats); $i++)
{
echo str_repeat(' -',$mycats[$i]['level']).$mycats[$i]['title'].'<br>';
}
echo '<br><br><br><br>';
//printing cats with its maps!!
for ($i=0; $i<count($mycats); $i++)
{
$cats->cat_map($mycats[$i]['id'],$mycats);
for ($a=0; $a<count($cats->cat_map); $a++) {
echo ($a < 1)  ? $cats->cat_map[$a]['title'] : ' -'.$cats->cat_map[$a]['title'];
}
echo (count($cats->cat_map) < 1) ? $mycats[$i]['title'].'<br>' : ' -'.$mycats[$i]['title'].'<br>';
}


?>

Link to comment
https://forums.phpfreaks.com/topic/70507-parentchile-category-help/
Share on other sites

echo '<br><br><br><br>';
//printing cats with its maps!!
for ($i=0; $i<count($mycats); $i++)
{
$cats->cat_map($mycats[$i]['id'],$mycats);
for ($a=0; $a<count($cats->cat_map); $a++) {
echo ($a < 1)  ? $cats->cat_map[$a]['title'] : ' -'.$cats->cat_map[$a]['title'];
}
echo (count($cats->cat_map) < 1) ? $mycats[$i]['title'].'<br>' : ' -'.$mycats[$i]['title'].'<br>';
}


?>

 

I think this part of it should be where I should be looking. It displays a tree structure as below. What I really want to know is, how can I make it so that it can work of the browser URL (ie. .com/browse.php?catId=0&show=subcats <--that would be all categories... .com/browse.php?catId=123&show=subcats <--that would be a seperate category and display all the sub cats.)

 

Please help.

Sam.

 

Output:

 

me
me -myselft
me -myselft -again!!
me -myselft -again!! -test1
me -myselft -again!! -test2
me -myselft -again!! -test2 -test3
me -myselft -again!! -test2 -test3 -test4

are you after something like this

 

<?php

$cats = array(
       #cat => array (desc, parent) 
        1 => array ('cat 1', 0),
        2 => array ('cat 2', 0),
        3 => array ('cat 3', 0),
        4 => array ('cat 1.1', 1),
        5 => array ('cat 1.2', 1),
        6 => array ('cat 1.3', 1),
        7 => array ('cat 2.1', 2),
        8 => array ('cat 2.2', 2),
        9 => array ('cat 3.1', 3),
        10 => array ('cat 1.2.1', 5),
        11 => array ('cat 1.2.2', 5),
        12 => array ('cat 1.1.1', 4),
        13 => array ('cat 1.1.2', 4),
);

function list_cats ($cats, $parent, $level=0)
{
    if ($parent && $level==0)
    {
        echo str_repeat(' ', $level*10), $cats[$parent][0], '<br />';
        $level++;
    }
    foreach ($cats as $k => $catdata)
    {
        if ($catdata[1] == $parent)
        {
            echo str_repeat(' ', $level*10), $catdata[0], '<br />';            
            list_cats ($cats, $k, $level+1);
        }
    }
}

list_cats ($cats,0);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.