<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Open source blog, linux, php, python, security &#187; recursiveadjacency list model</title>
	<atom:link href="http://www.alonon.net/tag/recursiveadjacency-list-model/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alonon.net</link>
	<description></description>
	<lastBuildDate>Wed, 02 Jun 2010 08:11:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>

   <image>
    <title>Open source blog, linux, php, python, security</title>
    <url>http://1.gravatar.com/avatar/5152c5736f5f8dd9570ffb2f9068e8ab?s=</url>
    <link>http://www.alonon.net</link>
   </image>
		<item>
		<title>Unlimited Subcategory with Adjacency List Model</title>
		<link>http://www.alonon.net/unlimited-subcategory-with-adjacency-list-model/</link>
		<comments>http://www.alonon.net/unlimited-subcategory-with-adjacency-list-model/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 16:38:52 +0000</pubDate>
		<dc:creator>ALonon</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[category system]]></category>
		<category><![CDATA[recursiveadjacency list model]]></category>
		<category><![CDATA[unlimited subcategory]]></category>

		<guid isPermaLink="false">http://www.alonon.net/?p=41</guid>
		<description><![CDATA[I 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 &#8230; <a href="http://www.alonon.net/unlimited-subcategory-with-adjacency-list-model/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I found two model two make to make category system with unlimited category one oft them is <a href="http://en.wikipedia.org/wiki/Tree_traversal">Tree traversal</a>, and other is <a href="http://en.wikipedia.org/wiki/Adjacency_list">Adjacency list </a>model.</p>
<p>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&#8217;s logic, i liked it <img src='http://www.alonon.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Let&#8217;s start with creating database;</p>
<pre lang="php">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);
</pre>
<p>We want to make sth like this:</p>
<p>-Computer</p>
<p>&#8212; PCS</p>
<p>&#8212;&#8212;Laptop</p>
<p>&#8212;&#8212;&#8212;Asus</p>
<p>-Phone</p>
<p>We use functions that call itself named recursive functions.</p>
<pre lang="php">
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-&gt;x = $this-&gt;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-&gt;x = $this-&gt;cat($select['id'], ($space+1), $this-&gt;x);  // function calls itself, with this, it finds subcategory of a category.
}
return $this-&gt;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-&gt;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;

}

}</pre>
<p>To use it</p>
<pre lang="php">$example = new CATEGORY;
$get_array_categories = $exapmle -&gt;get_cat(0);
print_r($get_array_categories);</pre>
<p>I wrote this to show you how you can make a category system with unlimited subcategory. If my codes don&#8217;t woking fine, please<br />
inform.</p>

	Tags: <a href="http://www.alonon.net/tag/category-system/" title="category system" rel="tag">category system</a>, <a href="http://www.alonon.net/tag/php/" title="Php" rel="tag">Php</a>, <a href="http://www.alonon.net/tag/recursiveadjacency-list-model/" title="recursiveadjacency list model" rel="tag">recursiveadjacency list model</a>, <a href="http://www.alonon.net/tag/unlimited-subcategory/" title="unlimited subcategory" rel="tag">unlimited subcategory</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.alonon.net/ubuntu-install-gd-support-for-apach/" title="Ubuntu install GD support for apache (December 11, 2009)">Ubuntu install GD support for apache</a> (0)</li>
	<li><a href="http://www.alonon.net/notice-use-of-undefined-constantvariable/" title="Notice: Use of undefined constant/variable (March 12, 2010)">Notice: Use of undefined constant/variable</a> (0)</li>
	<li><a href="http://www.alonon.net/mysql_insert_id/" title="mysql_insert_id (January 25, 2009)">mysql_insert_id</a> (0)</li>
	<li><a href="http://www.alonon.net/installing-apache-php-and-mysql-on-pardus/" title="installing apache php and mysql on Pardus (November 13, 2008)">installing apache php and mysql on Pardus</a> (0)</li>
	<li><a href="http://www.alonon.net/install-php-apache-mysql-phpmyadmin/" title="install php apache mysql phpmyadmin (January 1, 2009)">install php apache mysql phpmyadmin</a> (3)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.alonon.net/unlimited-subcategory-with-adjacency-list-model/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
