主页 > 编程资料 > PHP >
发布时间:2015-06-25 作者:网络 阅读:246次

第一段代码

得到要显示的类别.

一级目录放在categories[0],

二级目录放在categories[1],

三级目录放在categories[2],

....以此类推

不过一般最多到3级就不再往下分了.

Php代码  收藏代码
  1.     $cpath = array();  
  2.     if(!emptyempty($_REQUEST["parentId"])){  
  3.         //category path  
  4.         //get the category path  
  5. //put current category id in $cpath  
  6.         $current_category["parentId"] = $_REQUEST["parentId"];  
  7.   
  8. // put parent category id in $cpath           
  9. do{  
  10. array_unshift($cpath,$current_category["parentId"]);  
  11.             $data_category = array(  
  12.                 "id" => $current_category["parentId"]  
  13.             );  
  14.             $current_category = array();  
  15.             $current_category = pos($db_conn->search($db_category,$data_category));  
  16.         }while(!emptyempty($current_category["parentId"]));  
  17.     }  
  18.     //add the first level category parentId  
  19.     array_unshift($cpath,"null");  
  20.     // get all categories  
  21.     $categories_level_count = sizeof($cpath);  
  22.     for($i=0;$i $cpath[$i]  
  23.         );  
  24.         //get first level categories  
  25.         $categories[$i] = $db_conn->search($db_category,$data_category);  
  26. //      echo $db_category->search($data_category)."";  
  27.     }  





第二段代码是将菜单以

Html代码  收藏代码
  1. <ul>  
  2.   <li>  
  3.       <ul>  
  4.         ...  
  5.       </ul>  
  6.   </li>  
  7. </ul>  



的形式显示菜单

Php代码  收藏代码
  1. function show_left_navigation($i){  
  2.     global $categories,$cpath;  
  3.     if(emptyempty($categories[$i]))  
  4.         return;  
  5.     echo '<ul>';  
  6.     foreach($categories[$ias $id=>$category){  
  7.         echo '';  
  8.         if($id==end($cpath))  
  9.             echo '<a href="advancedSearch.php?parentId='.$id.'">'.$category["name"].'</a>';  
  10.         else  
  11.             echo '<a href="advancedSearch.php?parentId='.$id.'">'.$category["name"].'</a>';  
  12.         if(in_array($id,$cpath))  
  13.             show_left_navigation($i+1);  
  14.         echo '';  
  15.     }  
  16.     echo '</ul>';   
  17. }  
  18. show_left_navigation(0);  

 

关键字词: