php+html+mysql 构建一个多重分类选择列表的简单示例

前端之家收集整理的这篇文章主要介绍了php+html+mysql 构建一个多重分类选择列表的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面为各位大家介绍一下 PHP+html+MysqL 构建一个多重分类选择列表
不适用javascript,完全通过PHP实现多级列表选择,列表数据从数据库获取,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

/**
 * 多级列表选择,列表数据从数据库获取
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
# 
# 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>";

/***   来自编程之家 jb51.cc(jb51.cc)   ***/
原文链接:https://www.f2er.com/php/528874.html

猜你在找的PHP相关文章