ECSHOP简单三步实现导航二级分类

2016-07-07 14:55 来源:www.chinab4c.com 作者:ecshop专家

1.在page_header.lbi对应的位置(你想显示导航的位置)插入
(注意下面的"themes/模板名称/util.php"中的"模板名称"改成你模板文件夹的名称)
  1. <?php
  2. require_once("themes/模板名称/util.php");
  3. ?>
  4. <div>
  5. <p {if $navigator_list.config.index eq 1} {/if}><a href="../index.php">{$lang.home}</a></p>
  6. <ul>
  7. <!-- {foreach name=nav_middle_list from=$navigator_list.middle item=nav} -->
  8. <li onMouseOver="sw_nav(this,1);" onMouseOut="sw_nav(this,0);" {if $nav.active eq 1}{/if}>
  9. <a href="{$nav.url}" {if $nav.opennew eq 1}target="_blank" {/if}>{$nav.name}</a>
  10. <?php
  11. $subcates = get_subcate_byurl($GLOBALS['smarty']->_var['nav']['url']);
  12. if($subcates!=false)
  13. {
  14. if(count($subcates)>0)
  15. {
  16. echo "<div class='sub_nav'>";
  17. if($subcates)
  18. {
  19. foreach($subcates as $cate)
  20. {
  21. echo "<a href='".$cate['url']."' class='level_1'>".$cate['name']."</a>";
  22. }
  23. }
  24. echo "</div><iframe frameborder='0' scrolling='no' class='nomask'></iframe>";
  25. }
  26. }
  27. ?>
  28. </li>
  29. <!-- {/foreach} -->
  30. </ul>
  31. <script type="text/javascript">
  32. //初始化主菜单
  33. function sw_nav(obj,tag)
  34. {
  35. var subdivs = obj.getElementsByTagName("DIV");
  36. var ifs = obj.getElementsByTagName("IFRAME");

  37. if(subdivs.length>0)
  38. {
  39. if(tag==1)
  40. {
  41. subdivs[0].style.display = "block";
  42. ifs[0].style.display = "block";
  43. }
  44. else
  45. {
  46. subdivs[0].style.display = "none";
  47. ifs[0].style.display = "none";
  48. }
  49. }
  50. }
  51. </script>
  52. </div>
复制代码

2.CSS写法具体自己调试修改
  1. .header-menu p{ float:left;padding:1px 12px 1px 0;margin-top:-2px;}
  2. .header-menuul li{float:left;padding:1px 12px 1px 12px;margin-top:-2px;}
  3. .header-menu ul li a,.header-menu p a{color: #333;display:block;}
  4. .header-menu ul li a:hover,.header-menu p a:hover{color:#888;}
  5. .header-menu ul li.curs{background:#999;}
  6. .header-menu ul li.curs a{color:#fff;}

  7. .sub_nav{ background:#999;width:110px; position:absolute; z-index:5003; display:none;margin-left:-12px;}
  8. .nomask{ background:#fff; width:110px; height:50px; position:absolute; z-index:5002;display:none;margin-left:-12px;}
  9. .sub_nav a.level_1{ display:block;color:#fff;padding:6px 6px 6px 13px;font:11px Tahoma,Verdana,PMingLiU,Arial;border-bottom:1px dotted #D1D1D1;*border-bottom:1px dotted #D1D1D1 !important;*border-bottom:1px solid #A8A8A8;}
  10. .sub_nav a.level_1:hover{color:#fff;background:#55B46C;text-decoration:none;}
复制代码



把以下代码编辑成(util.php)解压出来拷贝到模板目录下
  1. <?php
  2. /**
  3. * 通过传入参数的url判断是否为目录分类,从而获取子菜单
  4. * @param string $url
  5. */
  6. function get_subcate_byurl($url)
  7. {
  8. $rs = strpos($url,"category");
  9. if($rs!==false)
  10. {
  11. preg_match("/d+/i",$url,$matches);
  12. $cid = $matches[0];
  13. $cat_arr = array();
  14. $sql = "select * from ".$GLOBALS['ecs']->table('category')." where parent_id=".$cid." and is_show=1";
  15. $res = $GLOBALS['db']->getAll($sql);

  16. foreach($res as $idx => $row)
  17. {
  18. $cat_arr[$idx]['id']= $row['cat_id'];
  19. $cat_arr[$idx]['name'] = $row['cat_name'];
  20. $cat_arr[$idx]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
  21. $cat_arr[$idx]['children'] = get_clild_list($row['cat_id']);
  22. }
  23. return $cat_arr;
  24. }
  25. else
  26. {
  27. return false;
  28. }
  29. }

  30. function get_clild_list($pid)
  31. {
  32. //开始获取子分类
  33. $sql_sub = "select * from ".$GLOBALS['ecs']->table('category')." where parent_id=".$pid." and is_show=1";
  34. $subres = $GLOBALS['db']->getAll($sql_sub);
  35. if($subres)
  36. {
  37. foreach ($subres as $sidx => $subrow)
  38. {
  39. $children[$sidx]['id']=$subrow['cat_id'];
  40. $children[$sidx]['name']=$subrow['cat_name'];
  41. $children[$sidx]['url']=build_uri('category', array('cid' => $subrow['cat_id']), $subrow['cat_name']);
  42. }
  43. }
  44. else
  45. {
  46. $children = null;
  47. }
  48. return $children;
  49. }
  50. ?>
复制代码

OK,基本完成了。。



不懂的可以加交流群,免费交流解答。QQ群180035291

回答:
支持楼主了多谢。 不过有效果图吗

<?php

这个已经不能使用了