<?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>The Tech Space &#187; recursion</title>
	<atom:link href="http://www.thetechspace.com/tag/recursion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thetechspace.com</link>
	<description>php, jquery, flash and resources</description>
	<lastBuildDate>Tue, 08 Nov 2011 08:47:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Get Unlimited Parent and Child in PHP</title>
		<link>http://www.thetechspace.com/2010/05/21/get-unlimited-parent-and-child-in-php/</link>
		<comments>http://www.thetechspace.com/2010/05/21/get-unlimited-parent-and-child-in-php/#comments</comments>
		<pubDate>Fri, 21 May 2010 08:12:39 +0000</pubDate>
		<dc:creator>Thurein</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://www.thetechspace.com/?p=321</guid>
		<description><![CDATA[Well, let me ask you first. Have you already known about recursion? If not, recursion mean reuse function in this function. How is it ? For eg Like function one_function ($var){ one_function(332); } OK , So , how to related with this post title ? I would like to say absolutely related with recursion. Let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Well, let me ask you first. Have you already known about recursion? If not, recursion mean reuse function in this function.</p>
<p>How is it ? For eg Like</p>
<pre class="brush:php">function one_function ($var){
           one_function(332);
}
</pre>
<p>OK , So , how to related with this post title ? I would like to say absolutely related with recursion. Let&#8217;s say, we would like to create category with parent and child.so, we have one table in database, category table. Table Structure Like:</p>

<table id="wp-table-reloaded-id-1-no-1" class="wp-table-reloaded wp-table-reloaded-id-1">
<thead>
	<tr class="row-1 odd">
		<th class="column-1">id</th><th class="column-2">name</th><th class="column-3">p_id</th>
	</tr>
</thead>
<tbody>
	<tr class="row-2 even">
		<td class="column-1">1</td><td class="column-2">Main Parent</td><td class="column-3">0</td>
	</tr>
	<tr class="row-3 odd">
		<td class="column-1">2</td><td class="column-2">Sub Parent</td><td class="column-3">1</td>
	</tr>
	<tr class="row-4 even">
		<td class="column-1">3</td><td class="column-2">Child</td><td class="column-3">2</td>
	</tr>
	<tr class="row-5 odd">
		<td class="column-1">4</td><td class="column-2">Parent 2</td><td class="column-3">0</td>
	</tr>
	<tr class="row-6 even">
		<td class="column-1">5</td><td class="column-2">Child 2</td><td class="column-3">4</td>
	</tr>
	<tr class="row-7 odd">
		<td class="column-1">6</td><td class="column-2">Child 3</td><td class="column-3">4</td>
	</tr>
	<tr class="row-8 even">
		<td class="column-1">7</td><td class="column-2">Child 4</td><td class="column-3">5</td>
	</tr>
</tbody>
</table>

<p><span id="more-321"></span>So, now I have created one table. I would like to get the out put like:</p>
<p>Main Parrent<br />
- Sub Parent<br />
&#8211; Child of sub parent<br />
Parent 2<br />
-  Child 2<br />
&#8211; Child of child 2<br />
- Child 3</p>
<p>So, how will you do it? How could we get it?</p>
<p>If you write the codes like, for eg :</p>
<pre class="brush:php">   $result = mysql_query ();
   while ( $row = mysql_fetch_assoc($result) ){

      echo $row[name];

      $result = mysql_query ();
      while ( $row = mysql_fetch_assoc($result) ){

            echo '- '.$row[name];
      }

   }
</pre>
<p>It would be work. But. If you have 3 or 4 or 5 levels and so on, you will got the trouble. So, what is the better way for that table? Shell we use recursion ?</p>
<p>I have written some code snippets like :</p>
<pre class="brush:php">
function display_child($parent, $level)
{
	$sql = "SELECT * from `category` WHERE p_id='$parent'";
	$result = mysql_query($sql);
	while ($row = mysql_fetch_array($result))
	{
		echo str_repeat('-',$level)." ".$row['name']."&lt;br /&gt;";
		display_child($row['id'], $level+1);
	}
}
</pre>
<p>Usage &#8211; display_child( parent_id, level )</p>
<pre class="brush:php">     display_children(0,0);
</pre>
<p>I hope it would be useful for some of your projects. Enjoy !!!!</p>
<h3><a href="http://www.thetechspace.com/?download=php_recursion" target="_blank">Download Source</a> ( size 1.1 kB)</h3>
<div class="shr-publisher-321"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.thetechspace.com/2010/05/21/get-unlimited-parent-and-child-in-php/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

