Jump to content

Multiple level categories


feri_soft

Recommended Posts

Hello, i am having problems to do unlimited multiple level categories. I have managed to create them in a tree menu and i have also managed to get them work in unlimited chained selects via jQuery but i want them to be just like xcart: Please go to http://www.x-cart.com/demo/admin/home.php click login and try add a new product for example. You will see a multiple select box like:

parent

parent::child

parent::child::child1

parent::child1::child2

or:

parent::child

parent::child::child1

parent::child::child1::child2

but i suppose the second will provide too long values.

I have the following code now:

http://pastebin.com/f66a1fb90

with the following db structure:

CREATE TABLE `category` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) character set cp1251 collate cp1251_bulgarian_ci NOT NULL,
  `parent` int(11) NOT NULL,
  `place` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=cp1251 AUTO_INCREMENT=7 ;

-- 
-- Dumping data for table `category`
-- 

INSERT INTO `category` VALUES (1, 'asdasd', 0, 1);
INSERT INTO `category` VALUES (2, 'asdasd1', 1, 2);
INSERT INTO `category` VALUES (3, 'rrrrrr', 2, 3);
INSERT INTO `category` VALUES (4, 'eeeeee', 3, 4);
INSERT INTO `category` VALUES (5, 'ssss', 2, 5);
INSERT INTO `category` VALUES (6, 'eeee', 2, 6);

this is modified code i found on the net. Please help me with this because i just keep getting different errors. Now the output of the code is:

asdasd

- asdasd::asdasd1

- asdasd1::rrrrrr

- rrrrrr::eeeeee

- asdasd1::ssss

- asdasd1::eeee

which is pretty close to what i need but i also need in front of rrrrr to have asdasd1 again. I need it this way so i can specify more than one cat for a product and i like this structure more than the one i first created - the old fashioned tree menu style within the select. I will be glad to hear any suggestions!  :)

Link to comment
https://forums.phpfreaks.com/topic/73004-multiple-level-categories/
Share on other sites

try

function cats($parent, $prefix='')
{
    $sql = "SELECT id, name FROM category WHERE parent = $parent";
    $res = mysql_query($sql);
    while (list($id, $name) = mysql_fetch_row($res))
    {
        echo $prefix . $name . '<br>';
        cats ($id, $prefix . $name . '::');
    }
}

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.