<?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>Prabeen&#039;s Blog &#187; Mysql</title>
	<atom:link href="http://prabeengiri.com.np/category/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://prabeengiri.com.np</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 04 Jan 2011 18:31:07 +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>Display nested categories inside Select Box , List ( UL, LI ) and BreadCrumb using single PHP Class</title>
		<link>http://prabeengiri.com.np/php/create-nested-categories-inside-drop-down-list-and-breadcrumb-using-single-php-class.html</link>
		<comments>http://prabeengiri.com.np/php/create-nested-categories-inside-drop-down-list-and-breadcrumb-using-single-php-class.html#comments</comments>
		<pubDate>Fri, 19 Mar 2010 08:37:31 +0000</pubDate>
		<dc:creator>Prabeen</dc:creator>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[breadcrumb]]></category>
		<category><![CDATA[multi- leveled categories]]></category>
		<category><![CDATA[mysqli]]></category>
		<category><![CDATA[nested categories]]></category>

		<guid isPermaLink="false">http://prabeengiri.com.np/?p=519</guid>
		<description><![CDATA[create multi leveled categories inside drop down list  bread crumb , nested categories over select box, drop down, menu list  with php ]]></description>
			<content:encoded><![CDATA[<p>Creating categories and its sub categories is always prevalent when we are developing simple to huge applications. Basically we  will have the requirement of creating one- leveled subcategory of parent category but when we come across creating multi-leveled subcategories , then it  seems little vague, complex to meet the requirement. Here I have made a simple php class to create multi-leveled nested categories on select box, List and creating &#8216;BreadCrumb&#8217; when navigated to those categories. Prime essence of this PHP class is use of <strong>recursive function</strong> ( calling parent function again and again ).<span id="more-519"></span></p>
<p><a href="http://demo.prabeengiri.com.np/nestedcategories/nestedcategories.html" target='_blank'><strong>DEMO</strong></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://demo.prabeengiri.com.np/nestedcategories/nestedcategories.zip" target='_blank'><strong>SOURCE CODE</strong></a></p>
<p>First lets create mysql table and insert data  inside it.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`categories`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`name`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`parentId`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1</pre></div></div>

<p>Here I have created the table &#8216;categories&#8217; with &#8216;id&#8217; as primary key and &#8216;autoincrement&#8217; set as true, &#8216;name &#8216; as varchar  and &#8216;parentId&#8217;  where reference  id of its parent category is stored.</p>
<p><strong>Note: If the category is  parent (.i.e which has no parent category)  then set its &#8216; parentId&#8217; as 0</strong></p>
<p>Lets insert the data into above table.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span>  <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`categories`</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`name`</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">`parentId`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category1'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category2'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category3'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category4'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category1.1'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category1.2'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category1.1.1'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category1.1.2'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category1.13'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category2.1'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category2.2'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'category1.1.2.1'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<div style='color:#353799'><strong>Wake up PHP ,  now its your turn&#8230;&#8230;.</strong></div>
<p><strong>PHP  Class for Database Connection:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Database 
<span style="color: #009900;">&#123;</span> 
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$username</span>	<span style="color: #339933;">=</span>	<span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$password</span>	<span style="color: #339933;">=</span>	<span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$database</span>	<span style="color: #339933;">=</span>	<span style="color: #0000ff;">&quot;multiplecategories&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$host</span> 		<span style="color: #339933;">=</span> 	<span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span> 
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$mysqli</span><span style="color: #339933;">;</span>  
	<span style="color: #000000; font-weight: bold;">function</span> database_connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span> 
		<span style="color: #666666; font-style: italic;">// connect to the database</span>
                <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mysqli</span> <span style="color: #339933;">=</span>  <span style="color: #000000; font-weight: bold;">new</span> Mysqli<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect_error</span><span style="color: #009900;">&#41;</span> 
		  <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Connection Error : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mysqli</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mysqli</span><span style="color: #339933;">;</span>  
&nbsp;
	<span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the above class I have used &#8216;<strong>MysqlI</strong>&#8216; ( <strong>Mysql Improved Extension</strong> ) for mysql operations. <strong>Mysqli</strong> is also <em> </em>one of the PHP API offering for  using mysql. The <strong>mysqli</strong> extension is     included with PHP versions 5 and later.</p>
<p>The <strong>mysqli</strong> extension has a number of benefits, the key enhancements over the <strong>mysql</strong> extension     being:</p>
<ul>
<li>Object-oriented interface</li>
<li>Support for Prepared Statements</li>
<li>Support for Multiple Statements</li>
</ul>
<p><strong>Note:If mysqli is confusing and not used before  then you can also use relevant mysql functions instead. </strong><br />
PHP Class : Nested Categories.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">/* Author Prabeen Giri &lt;http://prabeengiri.com.np&gt; &lt;prabeen.giri@gmail.com&gt; */</span>		
<span style="color: #000000; font-weight: bold;">class</span> NestedCategories <span style="color: #000000; font-weight: bold;">extends</span> Database
<span style="color: #009900;">&#123;</span> 
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$tableName</span> <span style="color: #339933;">;</span> 
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$linkHref</span>  <span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$_mysqli</span>   <span style="color: #339933;">;</span>  
        <span style="color: #666666; font-style: italic;">// constructor to set the value to the member variables</span>
	<span style="color: #000000; font-weight: bold;">function</span> NestedCategories<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$tableName</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$linkHref</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span> <span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span> 
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tableName</span> 		<span style="color: #339933;">=</span> 	<span style="color: #000088;">$tableName</span>  <span style="color: #339933;">;</span> 
&nbsp;
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$linkHref</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">linkHref</span>			<span style="color: #339933;">=</span> 	<span style="color: #000088;">$linkHref</span>	<span style="color: #339933;">;</span> 
&nbsp;
                <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_mysqli	        <span style="color: #339933;">=</span> 	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database_connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
	<span style="color: #009900;">&#125;</span> 
&nbsp;
        <span style="color: #666666; font-style: italic;">//creates the nested drop down ..................... </span>
	<span style="color: #000000; font-weight: bold;">public</span>  <span style="color: #000000; font-weight: bold;">function</span> createNestedDropDown<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$selectedId</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span>  <span style="color: #339933;">,</span> <span style="color: #000088;">$parentId</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>  <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span> 
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$counter</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span> 
			 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;select name='parentId'&gt;&quot;</span><span style="color: #339933;">;</span>  
			 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;option value='0'&gt;Parent&lt;/option&gt;&quot;</span><span style="color: #339933;">;</span>  
		<span style="color: #009900;">&#125;</span> 
&nbsp;
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from <span style="color: #006699; font-weight: bold;">{$this-&gt;tableName}</span> where parentId='<span style="color: #006699; font-weight: bold;">{$parentId}</span>'&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sql</span><span style="color: #339933;">;</span> 
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_mysqli<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sql</span> <span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_mysqli<span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// fetch all the result </span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span> 
				<span style="color: #666666; font-style: italic;">// if only selectedId parameter is provided.</span>
                        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$selectedId</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>			  
		        <span style="color: #000088;">$sel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$selectedId</span> ? <span style="color: #0000ff;">'selected=&quot;selected&quot;'</span><span style="color: #339933;">:</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> 
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;option value='<span style="color: #006699; font-weight: bold;">{$row-&gt;id}</span>' <span style="color: #006699; font-weight: bold;">{$sel}</span>&gt;&quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$counter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/option&gt;&quot;</span><span style="color: #339933;">;</span> 
			<span style="color: #666666; font-style: italic;">// calling same function , recusrion </span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createNestedDropDown</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$selectedId</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$counter</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> 
		<span style="color: #009900;">&#125;</span> 
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$counter</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/select&gt;&quot;</span> <span style="color: #339933;">;</span> 	
	<span style="color: #009900;">&#125;</span> 
	<span style="color: #666666; font-style: italic;">// to create the space on the drop down menu as the depth of the categories increases.</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> timer<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$counter</span>  <span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span> 
		<span style="color: #000088;">$space</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span> 
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$counter</span> <span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> 
			<span style="color: #000088;">$space</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;</span><span style="color: #339933;">;</span>  
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$space</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span> 
&nbsp;
        <span style="color: #666666; font-style: italic;">// creates the nested list with ul , li , ul .................. can be use for the navigation and menus.</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createNestedList<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parentId</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>  <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span> 
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$counter</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
			 <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;ul class='subMenu'&gt;&quot;</span><span style="color: #339933;">;</span>  
&nbsp;
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; select * from <span style="color: #006699; font-weight: bold;">{$this-&gt;tableName}</span> where parentId = <span style="color: #006699; font-weight: bold;">{$parentId}</span> &quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_mysqli<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sql</span> <span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_mysqli<span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span> 
		        <span style="color: #000088;">$res</span> 	<span style="color: #339933;">=</span> 	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_mysqli<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot; select * from <span style="color: #006699; font-weight: bold;">{$this-&gt;tableName}</span> where parentId = '&quot;</span> <span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;'&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
			<span style="color: #000088;">$tot</span> 	<span style="color: #339933;">=</span> 	<span style="color: #000088;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_rows</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$href</span> 	<span style="color: #339933;">=</span> 	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">linkHref</span><span style="color: #339933;">;</span> 
			<span style="color: #000088;">$ul</span> 	<span style="color: #339933;">=</span>  	<span style="color: #000088;">$tot</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> ? <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span> 
			<span style="color: #000088;">$_ul</span> 	<span style="color: #339933;">=</span> 	<span style="color: #000088;">$tot</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> ? <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">'&lt;/ul&gt;'</span><span style="color: #339933;">;</span> 
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&lt;a href='<span style="color: #006699; font-weight: bold;">{$href}</span><span style="color: #006699; font-weight: bold;">{$row-&gt;id}</span>'&gt;<span style="color: #006699; font-weight: bold;">{$row-&gt;name}</span>&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span> 
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$ul}</span>&quot;</span><span style="color: #339933;">;</span>  
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createNestedList</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$counter</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> 
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$_ul}</span>&quot;</span><span style="color: #339933;">;</span> 
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/li&gt;&quot;</span><span style="color: #339933;">;</span> 
		<span style="color: #009900;">&#125;</span> 
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$counter</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/ul&gt;&quot;</span> <span style="color: #339933;">;</span> 	
	<span style="color: #009900;">&#125;</span> 
&nbsp;
	<span style="color: #666666; font-style: italic;">//creates  breadCrumb as with all the parent categories. </span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> breadCrumb <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$categoryId</span><span style="color: #339933;">,</span> <span style="color: #000088;">$loop</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span> 
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; select * from <span style="color: #006699; font-weight: bold;">{$this-&gt;tableName}</span> where id = '<span style="color: #006699; font-weight: bold;">{$categoryId}</span>' &quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_mysqli<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sql</span> <span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_mysqli<span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$row</span> 	<span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> 
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$loop</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
		        <span style="color: #000088;">$a</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;a href='<span style="color: #006699; font-weight: bold;">{$this-&gt;linkHref}</span><span style="color: #006699; font-weight: bold;">{$row-&gt;id}</span>'&gt;<span style="color: #006699; font-weight: bold;">{$row-&gt;name}</span>&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span>
			<span style="color: #000088;">$a</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;strong&gt;<span style="color: #006699; font-weight: bold;">{$row-&gt;name}</span>&lt;/strong&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parentId</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> 
			<span style="color: #b1b100;">echo</span>  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">breadCrumb</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parentId</span><span style="color: #339933;">,</span><span style="color: #000088;">$loop</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;nbsp;&amp;raquo;&amp;nbsp;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span> 
			<span style="color: #b1b100;">echo</span>   <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span> 
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Invoke the Methods to render it in the browser. </strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
	<span style="color: #666666; font-style: italic;">// first  parameter is  category table name  and second is the url on the anchor tag on list and breadcrumb</span>
        <span style="color: #000088;">$nc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NestedCategories<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'?categoryId='</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Nested Categories over Drop Down </span>
	<span style="color: #666666; font-style: italic;">// if you need some category on drop down to be selected like when we want to edit the information .</span>
       <span style="color: #666666; font-style: italic;">// else omit the parameter .</span>
        <span style="color: #000088;">$nc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createNestedDropDown</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">9</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        or 
        <span style="color: #000088;">$nc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createNestedDropDown</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categoryId'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        or 
        <span style="color: #000088;">$nc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createNestedDropDown</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #666666; font-style: italic;">// Nested Categories over List </span>
	<span style="color: #000088;">$nc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createNestedList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #666666; font-style: italic;">//Bread Crumb; </span>
	<span style="color: #000088;">$nc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">breadCrumb</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'categoryId'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Note: If you are using createNestedList() method then script will automatically create the class as &#8216;subMenu&#8217; on the first ul that it generates or top &#8216;ul&#8217;. You can use javascript or css to hide the inner list or make animations to create drop down menu , navigations<br />
</strong><br />
I hope this class will easily help you  create the nested categories on drop down, list, and breadcrumb. If you have any problem in its usage then please free to respond me&#8230;&#8230;&#8230;.</p>
<p><a href="http://demo.prabeengiri.com.np/nestedcategories/nestedcategories.html" target='_blank'><strong>DEMO</strong></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://demo.prabeengiri.com.np/nestedcategories/nestedcategories.zip" target='_blank'><strong>SOURCE CODE</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://prabeengiri.com.np/php/create-nested-categories-inside-drop-down-list-and-breadcrumb-using-single-php-class.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Mysql &#8211;  Starting Auto Increment Primary Key with your own number</title>
		<link>http://prabeengiri.com.np/mysql/mysql-starting-auto-increment-primary-key-with-your-own-number.html</link>
		<comments>http://prabeengiri.com.np/mysql/mysql-starting-auto-increment-primary-key-with-your-own-number.html#comments</comments>
		<pubDate>Fri, 21 Aug 2009 07:06:53 +0000</pubDate>
		<dc:creator>Prabeen</dc:creator>
				<category><![CDATA[Mysql]]></category>

		<guid isPermaLink="false">http://prabeengiri.com.np/?p=55</guid>
		<description><![CDATA[Set Your own auto increment value for the primary key of  your table..]]></description>
			<content:encoded><![CDATA[<p>In mysql, we can also define our own number that primary key starts with if  it is set as the auto_increment.</p>
<p>Though it sounds as the complex task but its easy to implement.<span id="more-55"></span></p>
<p>First create table with one field as primary key and autoincrement attribute set.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`tableName`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`name`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1</pre></div></div>

<p>This will create the table on your database with two field &#8216;id&#8217; as datatype integer and &#8216;name&#8217; as datatype varchar.Here field &#8216;id&#8217; acts as the primary key for that particular table with its values auto increment.</p>
<p>By default &#8216;id&#8217; starts with value 1.</p>
<p>To make it start with our own number  ,   one line of code is enough.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span>  TABLENAME <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">200</span></pre></div></div>

<p>Subsequently , new value for our field &#8216;id&#8217; starts with 200&#8230;&#8230;&#8230;&#8230;&#8230;..</p>
]]></content:encoded>
			<wfw:commentRss>http://prabeengiri.com.np/mysql/mysql-starting-auto-increment-primary-key-with-your-own-number.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

