php 多級導航顯示
寫一次,想一次...重新做一次...? 這回就放這了..以後用直接過來copy.
第一段代碼
得到要顯示的類別.
一級目錄放在categories[0],
二級目錄放在categories[1],
三級目錄放在categories[2],
....以此類推
不過一般最多到3級就不再往下分了.
$cpath = array();if(!empty($_REQUEST["parentId"])){//category path//get the category path//put current category id in $cpath$current_category["parentId"] = $_REQUEST["parentId"];// put parent category id in $cpath do{array_unshift($cpath,$current_category["parentId"]);$data_category = array("id" => $current_category["parentId"]);$current_category = array();$current_category = pos($db_conn->search($db_category,$data_category));}while(!empty($current_category["parentId"]));}//add the first level category parentIdarray_unshift($cpath,"null");// get all categories$categories_level_count = sizeof($cpath);for($i=0;$i $cpath[$i]);//get first level categories$categories[$i] = $db_conn->search($db_category,$data_category);//echo $db_category->search($data_category)."";}
第二段代碼是將菜單以
的形式顯示菜單
function show_left_navigation($i){global $categories,$cpath;if(empty($categories[$i]))return;echo '';foreach($categories[$i] as $id=>$category){echo '';if($id==end($cpath))echo ''.$category["name"].'';elseecho ''.$category["name"].'';if(in_array($id,$cpath))show_left_navigation($i+1);echo '';}echo '
'; }show_left_navigation(0);
?
?