您好,登錄后才能下訂單哦!
ecshop導(dǎo)航要達(dá)到的目標(biāo):
一,比如上圖,當(dāng)我訪問三級分類,響應(yīng)式布局,這個欄目時,最頂級的元件這個分類,要高亮顯示
二,如果導(dǎo)航上面有商品或文章頻道, 并且他們有子欄目,則全自動顯示所有的子欄目.
三,如果這個導(dǎo)航有子分類,則統(tǒng)一顯示下拉三角標(biāo)志.
代碼如下
一,在includes/lib_main.php 文件中,修改掉或另外重命名并重定義一個這個get_navigator()函數(shù),修改后的內(nèi)容如下,另外get_categories_tree()這個函數(shù)為系統(tǒng)自帶的在lib_goods.php中
/** * 取得自定義導(dǎo)航欄列表 * @param string $type 位置,如top、bottom、middle * @return array 列表 */ function get_navigator($ctype = '', $catlist = array()) { $sql = 'SELECT * FROM '. $GLOBALS['ecs']->table('nav') . ' WHERE ifshow = \'1\' ORDER BY vieworder'; $res = $GLOBALS['db']->query($sql); $cur_url = substr(strrchr($_SERVER['REQUEST_URI'],'/'),1); if (intval($GLOBALS['_CFG']['rewrite'])) { if(strpos($cur_url, '-')) { preg_match('/([a-z]*)-([0-9]*)/',$cur_url,$matches); $cur_url = $matches[1].'.php?id='.$matches[2]; } } else { $cur_url = substr(strrchr($_SERVER['REQUEST_URI'],'/'),1); } $noindex = false; $active = 0; $has_suv=0; $navlist = array( 'top' => array(), 'middle' => array(), 'bottom' => array() ); while ($row = $GLOBALS['db']->fetchRow($res)){ if($row['ctype']=='a'){//如果是文章類的欄目 $row3=get_article_tree_for_nav($row['cid']);//列出所有子文章分類 $navlist[$row['type']][] = array( 'name' => $row['name'], 'opennew' => $row['opennew'], 'url' => $row['url'], 'ctype' => $row['ctype'], 'cid' => $row['cid'], 'has_suv' => 1,//文章類用1 'sub_nav' => $row3, ); }elseif($row['ctype']=='c'){//商品類的欄目 $row4=get_categories_tree($row['cid']);//使用系統(tǒng)默認(rèn)的函數(shù)即可.商品子類 $navlist[$row['type']][] = array( 'name' => $row['name'], 'opennew' => $row['opennew'], 'url' => $row['url'], 'ctype' => $row['ctype'], 'cid' => $row['cid'], 'has_suv' => 2,//與文章的導(dǎo)航做區(qū)分 'sub_nav' => $row4, ); }else{ $navlist[$row['type']][] = array(//單頁面等欄目,比如about.php等 'name' => $row['name'], 'opennew' => $row['opennew'], 'url' => $row['url'], 'ctype' => $row['ctype'], 'cid' => $row['cid'] ); } } /*遍歷自定義是否存在currentPage*/ foreach($navlist['middle'] as $k=>$v){ $condition = empty($ctype) ? (strpos($cur_url, $v['url']) === 0) : (strpos($cur_url, $v['url']) === 0 && strlen($cur_url) == strlen($v['url']));//單頁,如about.php等,$ctype的值沒有被傳入 //當(dāng)前網(wǎng)址與數(shù)據(jù)庫中循環(huán)出的網(wǎng)址相同 if ($condition)//如果相同 { $navlist['middle'][$k]['active'] = 1;//是否為當(dāng)前頁,追加到數(shù)組中 $noindex = true;//非首頁 } } if(!empty($ctype))//文章或商品頁面.在其控制器上傳入了本欄目的類型,商品類,用c表示,或文章類,用a表示 { //print_r($catlist);exit;即當(dāng)前訪問分類id foreach($catlist as $key => $val){ $parent_arr=get_top_cat_id_arr($val,$ctype);//當(dāng)前被訪問的分類id的所有父欄目組成的數(shù)組 //print_r($parent_arr);exit; foreach($navlist['middle'] as $k=>$v) { if(!empty($v['ctype']) && $v['ctype'] == $ctype && ($v['cid'] == $val ||in_array($v['cid'],$parent_arr))) {//in_array($v['cid'],$parent_arr),這句表示,如果本導(dǎo)航條上顯示的頻道id,包含在了當(dāng)前訪問的欄目的所有父欄目id數(shù)組中,則本導(dǎo)航條可高亮顯示 $navlist['middle'][$k]['active'] = 1;//高亮關(guān)鍵字 $noindex = true; } } } } if ($noindex == false) { $navlist['config']['index'] = 1; } //print_r($navlist);exit; return $navlist; }
二,同樣在includes/lib_main.php 文件中,增加以下函數(shù)
/** * 獲得指定分類同級的所有分類以及該分類下的子分類 * * @access public * @param integer $cat_id 分類編號 * @return array */ function get_article_tree($cat_id = 0){ if ($cat_id > 0)//$cat_id當(dāng)前分類 { $parent_id=get_top_art_cat_id($cat_id); } else { $parent_id = 0; } /* 判斷當(dāng)前分類中,是否是底級分類, 如果是取出底級分類上級分類, 如果不是取當(dāng)前分類及其下的子分類v */ $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('article_cat') . " WHERE parent_id = '$parent_id'";// if ($GLOBALS['db']->getOne($sql)|| $parent_id == 0){ /* 如果當(dāng)前分類有子分類,獲取當(dāng)前分類及其子分類 */ //$sql = 'SELECT cat_id, cat_name, sort_order FROM ' . $GLOBALS['ecs']->table('article_cat') ."WHERE cat_type=1 and cat_id = '$parent_id' ORDER BY sort_order ASC, cat_id ASC";//包含頂級本身,國內(nèi)新聞 $sql = 'SELECT cat_id, cat_name, sort_order FROM ' . $GLOBALS['ecs']->table('article_cat') ."WHERE cat_type=1 and parent_id = '$parent_id' ORDER BY sort_order ASC, cat_id ASC";//除排頂級分類,只顯示山東新聞,江蘇新聞及子分類 //兩種方式,這里得到的$row['cat_id']都是目標(biāo)catid,即需要高亮顯示的 $res = $GLOBALS['db']->getAll($sql); $cat_arr = array(); foreach ($res AS $row) { $cat_arr[$row['cat_id']]['id'] = $row['cat_id']; $cat_arr[$row['cat_id']]['name'] = $row['cat_name']; $cat_arr[$row['cat_id']]['url'] = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']); $parent_id2=get_top_art_cat_id($row['cat_id']);//得到最頂級父欄目id if ($parent_id2>0) { $cat_arr[$row['cat_id']]['cat_id'] =get_article_tree_child($row['cat_id'],$cat_id);//第二個參數(shù).傳入瀏覽器的當(dāng)前頁面分類號 $cat_id2=get_one_child_cat($row['cat_id']); $cat_arr[$row['cat_id']]['active']=$cat_arr[$row['cat_id']]['cat_id'][$cat_id2]['active'];//如果本欄目的其中任何一級子欄目是當(dāng)前訪問的欄目,則本欄目的所有父欄目 active=1,即可以高亮顯示. } } } //print_r($cat_arr);exit; return $cat_arr; } function get_article_tree_child($tree_id = 0,$cat_id){ $three_arr = array(); $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('article_cat') . ' WHERE parent_id = '.$tree_id; if ($GLOBALS['db']->getOne($sql) || $tree_id == 0) { $child_sql = 'SELECT cat_id, cat_name, parent_id' . ' FROM ' . $GLOBALS['ecs']->table('article_cat') . "WHERE parent_id = '$tree_id' ORDER BY sort_order ASC, cat_id ASC"; $res = $GLOBALS['db']->getAll($child_sql); foreach ($res AS $row) { $cat_cur=$cat_loop=array(); $active=0; $cat_cur=get_top_cat_id_arr($cat_id,'a');//當(dāng)前訪問的欄目的所有上級欄目id,所組成的數(shù)組 array_pop($cat_cur);//去除最頂級的欄目,防止干擾高亮 $cat_loop=get_top_cat_id_arr($row['cat_id'],'a');//循環(huán)時,本欄目的所有上級欄目id,所組成的數(shù)組 array_pop($cat_loop);//去除最頂級欄目id if(count(array_intersect($cat_cur,$cat_loop))>0){ //如果當(dāng)前訪問的欄目的父欄目數(shù)組與循環(huán)欄目得到的父欄目數(shù)組,有交集, //則訪問的欄目與其所有父欄目都是$active=1;方便前臺高亮 $active=1; } $three_arr[$row['cat_id']]['active'] = $active; $three_arr[$row['cat_id']]['id'] = $row['cat_id']; $three_arr[$row['cat_id']]['name'] = $row['cat_name']; $three_arr[$row['cat_id']]['url'] = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']); if (isset($row['cat_id']) != NULL) { $three_arr[$row['cat_id']]['cat_id'] = get_article_tree_child($row['cat_id'],$cat_id); } } } return $three_arr; } //得到其最上級分類的id function get_top_art_cat_id( $nid ){ $sql = "select parent_id from ".$GLOBALS['ecs']->table( "article_cat" )." where cat_id = ".$nid.""; $temp_id = 0; $pid = $GLOBALS['db']->getOne( $sql ); if ( 0 < $pid ) { $temp_id = get_top_art_cat_id( $pid ); return $temp_id; } $temp_id = $nid; return $temp_id; } //本文章或商品--分類對應(yīng)的所有上級分類的數(shù)組 function get_top_cat_id_arr( $nid ,$ctype='c'){ if($ctype=='c'){$table=$GLOBALS['ecs']->table( "category" );}else{$table=$GLOBALS['ecs']->table( "article_cat" );} $sql = "select parent_id from ".$table." where cat_id = ".$nid.""; $temp_id = 0; $temp_arr=array(); $pid = $GLOBALS['db']->getOne( $sql ); if ( $pid==0 ) { return $temp_arr; }else{ $temp_arr[]=$pid ; $sql2 = "select parent_id from ".$table." where cat_id = ".$pid.""; $pid2 = $GLOBALS['db']->getOne( $sql2 ); if($pid2==0){ return $temp_arr; }else{ $temp_arr[]=$pid2 ; $sql3 = "select parent_id from ".$table." where cat_id = ".$pid2.""; $pid3 = $GLOBALS['db']->getOne( $sql3 ); if($pid3==0){ return $temp_arr; }else{ $temp_arr[]=$pid3 ; $sql4 = "select parent_id from ".$table." where cat_id = ".$pid3.""; $pid4 = $GLOBALS['db']->getOne( $sql4 ); if($pid4==0){ return $temp_arr; }else{ $temp_arr[]=$pid4; $sql5 = "select parent_id from ".$table." where cat_id = ".$pid4.""; $pid5 = $GLOBALS['db']->getOne( $sql5 ); if($pid5==0){ return $temp_arr; }else{ $temp_arr[]=$pid5; $sql6 = "select parent_id from ".$table." where cat_id = ".$pid5.""; $pid6 = $GLOBALS['db']->getOne( $sql6 ); if($pid6==0){ return $temp_arr; }else{ $temp_arr[]=$pid6; return $temp_arr; }}}}}}}
三,相應(yīng)header模板中增加示例代碼,具體可自己修改,如果要增加層級,在模板上繼續(xù)嵌套即可.同時后面的這個函數(shù)也可以增加層級function get_top_cat_id_arr()
<ul id="nav2" class="nav2 clearfix"> <li class="nLi {if $navigator_list.config.index eq 1}on{/if}"> <a href="index.php"><span class="text">后易首頁</span></a> </li> <!-- {foreach name=nav_middle_list from=$navigator_list.middle item=nav} --> <li class="nLi {if $nav.active eq 1}on{/if}"> <a href="{$nav.url}" {if $nav.opennew eq 1}target="_blank" {/if}>{$nav.name} {if $nav.has_suv gt 0}<span class="arrow"></span>{/if}</a> {if $nav.has_suv eq 1} <ul class="sub"> <!-- {foreach name=sub_nav from=$nav.sub_nav item=child} --> <li><a href="{$child.url}">{$child.name}</a> <ul class="sub"> <!--{foreach from=$child.cat_id item=child1 }--> <li><a href="{$child1.url}">----{$child1.name}</a> <ul class="sub"> <!-- {foreach name=sub_nav from=$child1.cat_id item=child2} --> <li><a href="{$child2.url}">========{$child2.name}</a></li> <!-- {/foreach} --> </ul> </li> <!-- {/foreach} --> </ul> </li> <!-- {/foreach} --> </ul> {/if} {if $nav.has_suv eq 2} <ul class="sub"> <!-- {foreach name=sub_nav from=$nav.sub_nav item=child} --> <li><a href="{$child.url}">{$child.name}</a> <ul class="sub"> <!--{foreach from=$child.cat_id item=child1 }--> <li><a href="{$child1.url}">----{$child1.name}</a> <ul class="sub"> <!-- {foreach name=sub_nav from=$child1.cat_id item=child2} --> <li><a href="{$child2.url}">========{$child2.name}</a></li> <!-- {/foreach} --> </ul> </li> <!-- {/foreach} --> </ul> </li> <!-- {/foreach} --> </ul> {/if} </li><!-- {/foreach} --> </ul>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。