Unlimited Subcategory with Adjacency List Model
Posted by in PhpI found two model two make to make category system with unlimited category one oft them is Tree traversal, and other is Adjacency list model.
I will use adjacency list model because it is more understandable than tree traversal. In tree traversal model , insterting and editing categories is unclear , also this model is for bigger database. However, i advice you to look up Tree traversal’s logic, i liked it
.
Let’s start with creating database;
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(128) NOT NULL,
`top_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Tablo döküm verisi `category`
--
INSERT INTO `category` (`id`, `name`, `top_id`) VALUES
(1, 'Computer', 0),
(2, 'Laptop', 1),
(3, 'Asus', 2),
(4, 'Phones', 0);
We want to make sth like this:
-Computer
— PCS
——Laptop
———Asus
-Phone
We use functions that call itself named recursive functions.
class CATEGORY
{
function cat($cat_id, $space= 1, $x ='') // cat id, space to add "_" degree of categoreis times, list of categories
{
$sql = mysql_query("SELECT * FROM category WHERE top_id='$cat_id'");
while($select = mysql_fetch_assoc($sql))
{
$this->x = $this->x.str_repeat('_', $space).$select['name'].'-'; // making a list with all categories names. we add "_" to in front of category name. It adds degree of category times. For example asus deggre is 2 therefore it will be __asus, pda's degree is one therefore it will be _pda
$this->x = $this->cat($select['id'], ($space+1), $this->x); // function calls itself, with this, it finds subcategory of a category.
}
return $this->x; // This is list of all categories. Each category is separating with "-", be careful this is not an array , this is a string
}
function get_cat($cat_id)
{
$list = $this->cat($cat_tid);
$arr = explode('-', $list); // making array with categories
unset($arr[count($arr-1]);// deleting last element of array, because it is a blank
return $arr // return a array;
}
}
To use it
$example = new CATEGORY;
$get_array_categories = $exapmle ->get_cat(0);
print_r($get_array_categories);
I wrote this to show you how you can make a category system with unlimited subcategory. If my codes don’t woking fine, please
inform.
Related posts
You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

next time you post a code example make sure it WORKS!
it is working
I case anybody wants this to actually work, here’s how to fix it…
On line 4 change ” to ”
On line 22 change whole line to: unset($arr[(count($arr)-1)]);
On line 23 add a semicolon after $arr
On line 2 of how to use it, fix the spelling of word example.
Okay, the character mapping is still screwed up. On line 4 set $x to an empty string (two single quotes will do).
Perfect!! Thank you
Thank you, it works perfect!!
I was just added this:
$prethodni=0;
foreach($get_array_categories as $keyK => $valueK)
{
#echo $get_array_categories[$keyK].”";
$ovaj=substr_count($get_array_categories[$keyK], ‘>’);
if($prethodni<$ovaj){
echo "”;
echo “$ovaj”;
}elseif($prethodni==$ovaj){
echo “$ovaj”;
}elseif($prethodni>$ovaj){
echo str_repeat(”, $prethodni-$ovaj);
echo “$ovaj”;
}
$prethodni=$ovaj;
}
Intead _ I was used >