Tricks

how to make unlimited dynamic sub-categories in multiple forms
Making Unlimited Dynamic Sub-Categories

we will make the the unlimited dynamic sub-categories in two main forms

  • the root catgory method
  • the unlimited dynamic categories and sub-categories list method

see live demo

categories list system


this method give you all categories [ each parent catgory has its all sub-categories ]
function jooria_select($root, $depth, $sql,$selected = '') { 
     
     $row=0;
     while ($acat = mysql_fetch_array($sql)) 
     { 
          if ($acat['sub']== $root) 
          { 
          echo ($selected == $acat['id']) ? 
"<option selected value='".$acat['id']."'>" : "<option value='".$acat['id']."'>";
               $j=0; 

               while ($j<$depth) 
               {     
                    echo "  ";
                    $j++; 
               } 

               if($depth>0){
                    echo "-";
               }
               
               echo $acat['title']."</option>";
               
               mysql_data_seek($sql,0);  
                     //unlimited loop      
			         jooria_select($acat['id'], $depth+1,$sql,$selected);
			   echo $return;
          } 
          $row++; 
		 if(mysql_num_rows($sql)!=$row){
			 mysql_data_seek($sql,$row);
		 }
 			   
                 
     }
} 

Parameters

  • 'root' is the root catgory id that will open all its sub catgories
  • 'depth' this work on Arrangement of the sub catgories
  • 'sql' is the general mysql query it should be
  • "SELECT * FROM `jooria_categories` order by id asc"
    and determine the main not from the mysql query but from 'root'
  • 'selected' is extra option that enable insert selected='selected' in <option> tage ; just add the catgory id that you want to be selected.
For more clarification see the live example Above

the root catgory system

this method depends on the repetition of the function that attend the root-categories it repeats the process until you reach to sub = 0

function jooria_root($sub,$li = false){
    $sql = @mysql_query("SELECT * FROM `jooria_categories` WHERE id='$sub'");
    while($row = mysql_fetch_array($sql)){
        $id = $row['sub'];
            //unlimited loop to get the full root
            jooria_root($id,$li);
            
        print ($li) ? "<li>n" : " » ";
            echo "<a href='index.php?id=".$row['id']."'>".$row['title']."</a>n";
        print ($li) ? "</li>n" : "";	
    }

}

Parameters

  • 'sub' is the id of the current catgory
  • 'li' is an Extra option (if true will insert <li></li>) and this option just for the jQuery breadcrumbs plugin

how this function works?

  • first, it select the current catgory then it select the sub catgory of the current catgory from the catgories table
  • then it reapeat the first process in the sub catgory of the sub of the current catgory and so on

How i can use?

it's very easy just if you have an article ok and in the articles table there is an Field Called catgory_id assume there is an article have catgory_id = 3 and this is a sub of root id how i can get the root catgory? use some thing like this:
    $catgory_id = 3;
    echo jooria_root($catgory_id);
this will return:
» catgory 1 » catgory 2 » catgory 3 (the current)
For more clarification see the live example Above

the root catgory system with the jBreadCrumb

what is the jBreadCrumb?

It is smart in the sense that it collapses based upon the amount and length of the elements in the set. The breadcrumb uses a semitransparent .png overlay to achieve the gradient effect seen on the elements. Visually, it helps to show a "peek" at what is underneath and it Written by Jason Roy for CompareNetworks Inc.


how i can use jBreadCrumb?
just read this Explanation

i has been added it with my jooria_root system and you can see it in the live example

you can download the codes + all examples just look below at the the files of the tutorial
in: » Website Programming » Tricks | Posted on May 1st, 2010 | last update on May 2nd, 2010 | views 8,224
you can see also [tutorials]:
  1. Making Unlimited Dynamic Sub-Categories
About the author
i'm Muhammad from egypt i love doing one thing 'web programming & designing'
contributors