<?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>iCreativeLabs blog &#187; code</title>
	<atom:link href="http://icreativelabs.com/blog/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://icreativelabs.com/blog</link>
	<description>We LOVE our JOBS and we SHARE for you</description>
	<lastBuildDate>Fri, 30 Mar 2012 12:04:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Got Problem with FCKEditor In Jamit</title>
		<link>http://icreativelabs.com/blog/2008/05/got-problem-with-fckeditor-in-jamit/</link>
		<comments>http://icreativelabs.com/blog/2008/05/got-problem-with-fckeditor-in-jamit/#comments</comments>
		<pubDate>Thu, 22 May 2008 22:11:37 +0000</pubDate>
		<dc:creator>agoes82</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[fck editor]]></category>

		<guid isPermaLink="false">http://icreativelabs.com/blog/?p=15</guid>
		<description><![CDATA[This late of night for writing something useful, but i try to describe the problem and solver in just a few paragraph. This FCKeditor case. When I try customizing Jamit Job Board I got error on admin Page. This error only came on FCKEditor, you can see the error code below uncaught exception: Permission denied [...]]]></description>
			<content:encoded><![CDATA[<p>This late of night for writing something useful, but i try to describe the problem and solver in just a few paragraph.</p>
<p>This FCKeditor case. When I try customizing Jamit Job Board I got error on admin Page. This error only came on FCKEditor, you can see the error code below</p>
<pre class="brush: jscript;">
uncaught exception: Permission denied to set property Window.FCK_STATUS_NOTLOADED
....
fckeditor.html (line 183)
uncaught exception: Permission denied to get property Window.FCKeditorAPI
</pre>
<p>For  your attention in our tested case everything run smoothly, but when I test again after my client said they found some error like before I got some error like my client said.</p>
<p>Hmmmm what&#8217;s wrong with the code&#8230;</p>
<p>I learn every code and html output, everything not on mess, all tag already close. Trying on uncle google an still can&#8217;t found anything.</p>
<p>At last I found the problem when I watch every case in before. The problem it&#8217;s on HTTP address. In wrong version i used www before the address and the correct setting it&#8217;s without www.</p>
<p>For solved the address problem I used .htaccess rewrite</p>
<pre class="brush: jscript;">
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.eastafricajobs.com$
RewriteRule (.*)$ http://eastafricajobs.com/$1 [R=301,L]
</pre>
<p>That&#8217;s code will rewrite your url and will redirect www.eastafricajobs.com to eastafricajobs.com</p>
<p>Problem done and one jobs has solved</p>
<img src="http://icreativelabs.com/blog/?ak_action=api_record_view&id=15&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://icreativelabs.com/blog/2008/05/got-problem-with-fckeditor-in-jamit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adjacency list tree on MySQL</title>
		<link>http://icreativelabs.com/blog/2008/05/adjacency-list-tree-on-mysql/</link>
		<comments>http://icreativelabs.com/blog/2008/05/adjacency-list-tree-on-mysql/#comments</comments>
		<pubDate>Thu, 15 May 2008 03:27:04 +0000</pubDate>
		<dc:creator>oky_pm</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://icreativelabs.com/blog/?p=12</guid>
		<description><![CDATA[sebetulnya ini kupipas dari blog sayah *hayoo tebak blog saya apa coba..* berhubung dari kemaren dikejar2 si &#8220;luffy&#8221; untuk posting,apa boleh buat&#8230; *huh!* Modelling hierarchy data in relational database is hard. Adjacency list is one of few method to modelling this data to SQL. if you do not know about adjacency list, or you want [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>sebetulnya ini kupipas dari blog sayah *hayoo tebak blog saya apa coba..* berhubung dari kemaren dikejar2 si &#8220;luffy&#8221; untuk posting,apa boleh buat&#8230; *huh!*</p></blockquote>
<p><a href="http://en.wikipedia.org/wiki/Hierarchical_model">Modelling hierarchy data</a> in relational database is hard.</p>
<p>Adjacency list is one of few method to modelling this data to SQL.<br />
if you do not know about adjacency list, or you want to know other method than adjacency list, you might wanto to read <a href="http://www.sqlsummit.com/AdjacencyList.htm">Joe Celko&#8217;s Trees and Hierarchies in SQL for Smarties</a></p>
<p>Adjacency list is known with its &#8220;easy to insert but hard to retrieve&#8221;.</p>
<p><span id="more-12"></span></p>
<p>In this post, i want to share my solution on hierarchy data in MySQL with Adjacency list method, in order to have these method work, you need MySQL with TRIGGER support (i use 5.0.45 version).</p>
<p>Lets take a look on dummy data:</p>
<pre>Director
|-- Secretary
|
|-- Treasury Mgr
|    |-- Employee 1
|    |-- Employee 2
|
|-- Sales Mgr
     |-- Region 1 Mgr
     |    |-- Employee 3
     |    |-- Employee 4
     |    |-- Employee 5
     |
     |-- Region 2 Mgr
          |-- Subregion A Mgr
          |    |-- Employee 6
          |    |-- Employee 7
          |-- Subregion B Mgr
               |-- Employee 8
               |-- Employee 9</pre>
<p>Now, create table</p>
<pre class="brush: sql;">mysql&gt; CREATE TABLE `employee` (
&gt; `emp_id` INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
&gt; `emp_parent_id` INT NULL,
&gt; `name` VARCHAR(200) NOT NULL,
&gt; FOREIGN KEY(`emp_parent_id`) REFERENCES `employee`(`emp_id`)
&gt; ) ENGINE=INNODB DEFAULT CHARSET=UTF8;</pre>
<p>Then, to make life easier when retrieving data we need additional table (helper table) : a path table</p>
<pre class="brush: sql;">mysql&gt; CREATE TABLE `employee_path`(
&gt; `emp_id` INT PRIMARY KEY NOT NULL,
&gt; `path` TEXT,
&gt; FOREIGN KEY(`emp_id`) REFERENCES `employee`(`emp_id`)
&gt; ) ENGINE=INNODB DEFAULT CHARSET=UTF8;</pre>
<p>after that, we need trigger to automate path builder everytime employee record inserted or updated.</p>
<pre class="brush: sql;">mysql&gt; DELIMITER //
mysql&gt; CREATE TRIGGER `makeEmpTree` AFTER INSERT ON `site`
&gt; FOR EACH ROW BEGIN
&gt;  DECLARE `parent_path TEXT DEFAULT '';
&gt;  SELECT `path` INTO `parent_path` FROM `employee_path` WHERE `emp_id` = NEW.`emp_id`;
&gt;  INSERT INTO `employee_path` VALUES(
&gt;    NEW.`emp_id`,
&gt;    CONCAT(IF(LENGTH(`parent_path`) &lt; 2, '/', `parent_path`), NEW.`emp_id`, '/')
&gt;  );
&gt; END;
&gt; //
mysql&gt; CREATE TRIGGER `updateEmpTree` BEFORE UPDATE ON `site`
&gt; FOR EACH ROW BEGIN
&gt;  DECLARE `old_path` TEXT DEFAULT '';
&gt;  DECLARE `new_path` TEXT DEFAULT '';
&gt;  SELECT `path` INTO `old_path` FROM `employee_path` WHERE `emp_id` = NEW.`emp_id`;
&gt;  SELECT `path` INTO `new_path` FROM `employee_path` WHERE `emp_id` = NEW.`emp_parent_id`;
&gt;  UPDATE `employee_path` SET `path` =
&gt;    REPLACE(`path`, `old_path`,
&gt;      CONCAT(IF(LENGTH(`new_path`) &lt; 2, '/', `new_path`), NEW.`emp_id`, '/') )
&gt;    WHERE LEFT(`path`, LENGTH(`old_path`)) = `old_path`;
&gt; END;
&gt; //
mysql&gt; DELIMITER ;
</pre>
<p>done!</p>
<p>lets take hands on data:</p>
<p><strong>1st &#8211; INSERTING DATA</strong></p>
<pre class="brush: sql;">mysql&gt; INSERT INTO `employee` VALUES
&gt; (DEFAULT, NULL, 'Director'), (DEFAULT, 1, 'Secretary'),
&gt; (DEFAULT, 1, 'Treasury Mgr'), (DEFAULT, 3, 'Employee 1'),
&gt; (DEFAULT, 3, 'Employee 2'), (DEFAULT, 1, 'Sales Mgr'),
&gt; (DEFAULT, 6, 'Region 1 Mgr'), (DEFAULT, 7, 'Employee 3'),
&gt; (DEFAULT, 7, 'Employee 4'), (DEFAULT, 7, 'Employee 5'),
&gt; (DEFAULT, 6, 'Region 2 Mgr'), (DEFAULT, 11, 'Subregion A Mgr'),
&gt; (DEFAULT, 12, 'Employee 6'), (DEFAULT, 12, 'Employee 7'),
&gt; (DEFAULT, 11, 'Subregion B Mgr'), (DEFAULT, 15, 'Employee 8'),
&gt; (DEFAULT, 15, 'Employee 9');</pre>
<p>should be returning output such as</p>
<pre class="brush: sql;">Query OK, 17 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0</pre>
<p>now, check on path table, it should have content like below:</p>
<pre class="brush: sql;">mysql&gt; SELECT * FROM `employee_path`;
+--------+----------------+
| emp_id | path           |
+--------+----------------+
|      1 | /1/            |
|      2 | /1/2/          |
|      3 | /1/3/          |
|      4 | /1/3/4/        |
|      5 | /1/3/5/        |
|      6 | /1/6/          |
|      7 | /1/6/7/        |
|      8 | /1/6/7/8/      |
|      9 | /1/6/7/9/      |
|     10 | /1/6/7/10/     |
|     11 | /1/6/11/       |
|     12 | /1/6/11/12/    |
|     13 | /1/6/11/12/13/ |
|     14 | /1/6/11/12/14/ |
|     15 | /1/6/11/15/    |
|     16 | /1/6/11/15/16/ |
|     17 | /1/6/11/15/17/ |
+--------+----------------+
17 rows in set (0.00 sec)
</pre>
<p><strong>2nd &#8211; BASIC SELECT QUERY</strong><br />
<em>Get all root nodes</em><br />
Useful when hierarchy has multiple roots. (This sample has only 1 root)</p>
<pre class="brush: sql;">mysql&gt; SELECT * FROM `employee` WHERE `emp_parent_id` IS NULL;</pre>
<p><em>Get Sibling node</em><br />
sample query: get <a href="http://www.google.com/search?q=define%3A+sibling">sibling</a> of Treasury Manager (id = 3)</p>
<pre class="brush: sql;">mysql&gt; SELECT e.* FROM `employee` e, `employee` e2
&gt; WHERE
&gt;   e.`emp_parent_id` = e2.`emp_parent_id` AND
&gt;   e2.`emp_id` = 3 AND
&gt;   e.`emp_id` &lt;&gt; e2.`emp_id`;
</pre>
<p><em>Get Full Tree Nodes From Known Root Node</em><br />
sample query:get full tree nodes where root is director (emp_id = 1)<br />
Just replace number &#8217;1&#8242; on &#8216;/1/%&#8217; with another root id if table has multiple root node</p>
<pre class="brush: sql;">mysql&gt; SELECT `emp_id`, `path`
&gt; FROM `employee_path`
&gt; WHERE `path` LIKE('/1/%')
&gt; ORDER BY `path` ASC;</pre>
<p><em>Get Branch Nodes</em><br />
sample query: get branch nodes of Sales Mgr (id = 6)<br />
Actually, query below is generalization of query above <img src='http://icreativelabs.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<pre class="brush: sql;">mysql&gt; SELECT
&gt;  e.`emp_id` AS 'Most Root Node Id',
&gt;  e2.`emp_id,
&gt;  e2.`name`
&gt;  p.`path`
&gt; FROM
&gt;  `employee` e, `employee` e2,
&gt;  `employee_path` p, `employee_path` p2
&gt; WHERE
&gt;  e.emp_id = 6 AND
&gt;  p2.`emp_id` = e.`emp_id AND
&gt;  p.`path` LIKE(CONCAT(p2.`path`,'%')) AND
&gt;  e2.`emp_id = p.`emp_id`
&gt; ORDER BY p.`path` ASC;
</pre>
<blockquote><p>Note:<br />
- This query is not optimized!<br />
- This query is generic query, so you could have fun with it.<br />
e.g. : replace &#8216;e.emp_id = 6&#8242; with :<br />
+ &#8216;e.`emp_id` = 1&#8242; and you have same result with query &#8220;get full tree from known root node<br />
+ &#8216;e.`emp_parent_id` IS NULL&#8217; and you have all tree grouped by each root node</p></blockquote>
<p><em>get hierarchy style</em><br />
To have hierarchy style we could use query below:</p>
<pre class="brush: sql;">mysql&gt; SELECT
&gt;  e.`emp_id`,
&gt;  CONCAT(
&gt;    REPEAT('  ',
&gt;      (LENGTH(p.`path`) - LENGTH(REPLACE(p.`path`,'/','')),
&gt;      e.`name`
&gt;    ) AS tree
&gt; FROM `employee` e
&gt; LEFT JOIN `employee_path` p ON e.`emp_id` = p.`emp_id`
&gt; ORDER BY p.`path ASC;

+--------+---------------------------+
| emp_id | tree                      |
+--------+---------------------------+
|      1 |     Director              |
|      2 |       Secretary           |
|      3 |       Treasury Mgr        |
|      4 |         Employee 1        |
|      5 |         Employee 2        |
|      6 |       Sales Mgr           |
|     11 |         Region 2 Mgr      |
|     12 |           Subregion A Mgr |
|     13 |             Employee 6    |
|     14 |             Employee 7    |
|     15 |           Subregion B Mgr |
|     16 |             Employee 8    |
|     17 |             Employee 9    |
|      7 |         Region 1 Mgr      |
|     10 |           Employee 5      |
|      8 |           Employee 3      |
|      9 |           Employee 4      |
+--------+---------------------------+

17 rows in set (0.00 sec)</pre>
<p><strong>3rd. Manipulating Node</strong><br />
To know whether those query below success or not, you could check yourself by comparing the result from &#8220;hierarchy style query&#8221; before and after execute each query below.<br />
<em>Move leaf node to another node</em><br />
Sample query: move employee 3 to treasury Mgr</p>
<pre class="brush: sql;">mysql&gt; UPDATE `employee` SET `emp_parent_id` = 3 WHERE `emp_id` = 8;</pre>
<p><em>Move leaf node to become root node</em><br />
Sample query: make Region 2 Mgr become a root node (actually, IMO, this action should not happen in the context of employee ;p )</p>
<pre class="brush: sql;">mysql&gt; UPDATE `employee` SET `emp_parent_id` = NULL WHERE `emp_id` = 11;</pre>
<p><em>Move branch node to another branch</em><br />
Sample query: move Subregion A Mgr to Sales Mgr</p>
<pre class="brush: sql;">mysql&gt; UPDATE `employee` SET `emp_parent_id` = 6 WHERE `emp_id` = 12;</pre>
<p><strong>4th. Note</strong></p>
<blockquote><p>Testing can only prove the presence of bugs, not their absence.<br />
&#8211;Edsger W. Dijkstra</p></blockquote>
<p>This appoarch, of course, have a limitation.<br />
Limitation, or bug, i know was (please share, if you found another limitation/bug) :</p>
<ul>
<li>There is no checking when moving a node to its decendant. (I&#8217;m still thinking about check whether the new parent node is its decendant or not in before update trigger)</li>
<li>This approach does not cover node weight yet.(if you have no idea about weight, take a look at &#8220;hierarchy style&#8221; query result and compare it with sample hierarchy on the top. and ask to yourself, why Region 1 Mgr on the result become below Region 2 Mgr? ;p)</li>
</ul>
<p>PS: I found postgresql developer <a href="http://www.depesz.com/index.php/2008/04/11/my-take-on-trees-in-sql/">wrote about another different approach of adjacency list in his blog</a>. you might want to read about it.</p>
<img src="http://icreativelabs.com/blog/?ak_action=api_record_view&id=12&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://icreativelabs.com/blog/2008/05/adjacency-list-tree-on-mysql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>code snippet ready</title>
		<link>http://icreativelabs.com/blog/2008/05/code-snippet-ready/</link>
		<comments>http://icreativelabs.com/blog/2008/05/code-snippet-ready/#comments</comments>
		<pubDate>Wed, 14 May 2008 04:27:06 +0000</pubDate>
		<dc:creator>anggi</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://icreativelabs.com/blog/?p=6</guid>
		<description><![CDATA[Tes source code CSS : #button { font-weight: bold; border: 2px solid #fff; } PHP : &#60;?php echo 'hello world!'; ?&#62; SQL : SELECT a.* FROM a LEFT JOIN b ON a.id=b.a_id;]]></description>
			<content:encoded><![CDATA[<p>Tes source code</p>
<p><strong>CSS</strong> :</p>
<pre class="brush: css;">#button {
font-weight: bold;
border: 2px solid #fff;
}</pre>
<p><strong>PHP</strong> :</p>
<pre class="brush: php;">&lt;?php echo 'hello world!'; ?&gt;</pre>
<p><strong>SQL</strong> :</p>
<pre class="brush: sql;">SELECT a.* FROM a LEFT JOIN b ON a.id=b.a_id;</pre>
<img src="http://icreativelabs.com/blog/?ak_action=api_record_view&id=6&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://icreativelabs.com/blog/2008/05/code-snippet-ready/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

