Thank you for your input, NogDog and sneakyimp !
Here's some code I have been given from elsewhere ...
The table itself:
CREATE TABLE `category` (
`category_id` int(1) unsigned NOT NULL AUTO_INCREMENT,
`category_title` varchar(100) NOT NULL,
`category_parent_id` int(1) unsigned NOT NULL,
PRIMARY KEY (`category_id`))
ENGINE=InnoDB;
The PHP Code:
<?php
function category_list($category_parent_id = 0)
{
// build our category list only once
static $cats;
if (!is_array($cats)) {
$sql = 'SELECT * FROM `category`';
$res = mysql_query($sql);
$cats = array();
while ($cat = mysql_fetch_assoc($res)) {
$cats[] = $cat;
}
}
// populate a list items array
$list_items = array();
foreach($cats as $cat) {
// if not a match, move on
if (( int )$cat['category_parent_id'] !== ( int )$category_parent_id) {
continue;
}
// open the list item
$list_items[] = '<li>';
// construct the category link
$list_items[] = '<a href="#' . $cat['category_id'] . '">';
$list_items[] = $cat['category_title'];
$list_items[] = '</a>';
// recurse into the child list
$list_items[] = category_list($cat['category_id']);
// close the list item
$list_items[] = '</li>';
}
// convert to a string
$list_items = implode('', $list_items);
// if empty, no list items!
if ('' == trim($list_items)) {
return '';
}
// ...otherwise, return the list
return '<ul>' . $list_items . '</ul>';
}
The PHP function that throws out the tree:
echo category_list();
But, I'm a bit stuck.
Created a table exactly as directed by the coder.
Entered a few categories in it to test drive and this is how they look in MySQL:
"category_id";"category_title";"category_parent_id"
"1";"Category 1";"0"
"2";"Category 1 - Level 1";"1"
"3";"Category 1 - Level 2";"2"
"4";"Category 1 - Level 3";"3"
"5";"Category 1 - Level 4";"4"
"6";"Category 1 - Level 5";"5"
"7";"Category 1 - Level 6";"6"
"8";"Category 2";"0"
"9";"Category 2 - Level 1";"8"
"10";"Category 2 - Level 2";"9"
"11";"Category 2 - Level 3";"10"
"12";"Category 2 - Level 4";"11"
"13";"Category 2 - Level 5";"12"
"14";"Category 3";"0"
But when I run the code, it shows only "Category 1" and it's six levels of sub categories.
Does NOT show the "Category 2" onwards.
Any idea as to what I have done wrong?
I discussed this with the coder and he tested him but he seems to get proper results.
Please help !
Thank you ! ....
Kind Regards,
Xeirus.