CoderZone.org

Category: >> PHP Code >> Hierarchical categories browser Bookmark and Share

<< lastnext >>

Snippet Name: Hierarchical categories browser

Description: A very simple way to build and do a hierarchical HTML categories browser without javascript- just HTML, PHP and mySql.

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009

<?php
 
/* 
# 
# Table structure for table 'res_cat' 
# 
 
CREATE TABLE res_cat ( 
   ID int(32) DEFAULT '0' NOT NULL , 
   ID_uname char(40) NOT NULL, 
   UNIQUE ID (ID) 
); 
 
# 
# Dumping data for table 'res_cat' 
# 
 
INSERT INTO res_cat VALUES( '1', 'head1'); 
INSERT INTO res_cat VALUES( '101', 'cat11'); 
INSERT INTO res_cat VALUES( '102', 'cat12'); 
INSERT INTO res_cat VALUES( '103', 'cat13'); 
INSERT INTO res_cat VALUES( '10101', 'subcat111'); 
INSERT INTO res_cat VALUES( '10102', 'subcat112'); 
INSERT INTO res_cat VALUES( '10103', 'subcat113'); 
INSERT INTO res_cat VALUES( '1010101', 'subcat1111'); 
INSERT INTO res_cat VALUES( '1010102', 'subcat1112'); 
INSERT INTO res_cat VALUES( '1010103', 'subcat1113'); 
INSERT INTO res_cat VALUES( '10201', 'subcat121'); 
INSERT INTO res_cat VALUES( '10202', 'subcat122'); 
INSERT INTO res_cat VALUES( '10203', 'subcat123'); 
INSERT INTO res_cat VALUES( '10301', 'subcat131'); 
INSERT INTO res_cat VALUES( '10302', 'subcat132'); 
INSERT INTO res_cat VALUES( '10303', 'subcat133'); 
INSERT INTO res_cat VALUES( '2', 'head2'); 
INSERT INTO res_cat VALUES( '201', 'cat21'); 
INSERT INTO res_cat VALUES( '202', 'cat22'); 
INSERT INTO res_cat VALUES( '203', 'cat33'); 
etc ... 
*/ 
$mysql_link = mysql_connect("localhost", "user", "passwd"); 
mysql_select_db("res_cat", $mysql_link); 
echo "<html><body>" ; 
 
function listrub ($id , $exploseID   ) 
{ 
global $PHP_SELF ; 
global $mysql_link  ; 
$maxi = ($id*100)+100 ; 
$mini = $id*100 ; 
$query = "SELECT * FROM res_cat  where ID < $maxi and ID >  $mini    " ;   
echo "<ol>"; 
if(      $mysql_result = mysql_query($query , $mysql_link)   ) 
{ 
while ($row = mysql_fetch_object($mysql_result)) 
    { 
// you have to link some action on leaves of course 
echo "<li> <a href='$PHP_SELF?ID_rub=$row->ID' >   $row->ID_uname </a>  </li> " ; 
$testID = ($exploseID - ($exploseID  %100 ))/100 ; //  WARNING this is ugly .. only 3 level 
if ( $testID == $row->ID || $exploseID == $row->ID) 
         { 
         listrub ($row->ID , $exploseID ) ; 
        } 
    } 
} 
echo "</ol>"; 
} 
$father =999999999;  // a big number   
// get the grand...grand father 
if ( $ID_rub >100 ) 
{ 
$father = $ID_rub ; 
while ( $father >100 )    { $father= ( $father - ( $father %100) )/100 ; } 
} 
else{$father= $ID_rub ;} 
listrub ($father , $ID_rub ,$mysql_link ) ;   
echo "</body></html>"; 
 
?> 
 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 8