Hierarchical Categories Browser

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

<?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>";
 
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.