doubiankang2845 2016-10-09 13:27
浏览 40

PHP递归函数问题。

I am having a php recursive function to display my categories with sub, sub-sub category.

Its look like below:

function displayList(&$cats, $parent, $current=0, $level=0) {
  if ($parent==0) {
    foreach ($cats[$parent] as $id=>$nm) {
        displayList($cats, $id, $current);
    }
  }
  else {
    echo "<ul>
";        
    foreach ($cats[$parent] as $id=>$nm) {
      $sel = $current==$id ? ' active' : '';        

      //$indent = str_repeat('&ndash; ', $level);
      echo "<li><a href='' class='$sel'>$nm</a>
";
      if (isset($cats[$id])) {
          displayList($cats, $id, $current, $level+1);  //increment level
      }
      echo '</li>';
    }
    echo "</ul>
";
  }  
}

Here I want to add a CSS class for each parent <li>, if it has a sub, sub-sub categories.

My HTML is something like this:

<ul>
  <li><a href="product_category.html">Party Balloons</a></li>
  <li><a href="">Party Tableware</a></li>
  <li class="parent"><a href="">Party Decorations</a>
  <ul>
    <li><a href="">Lorem ipsum dolor sit amet </a></li>
    <li class="parent"><a href="">Conse ctetur adipisicing </a>
      <ul>
        <li><a href="">Dolore magna aliqua</a></li>
        <li><a href="">Ut labore et </a></li>
      </ul>
    </li>
    <li><a href="">Elit sed do eiusmod tempor</a></li>
    <li><a href="">Incididunt ut labor</a></li>
  </ul>
  </li>
  <li><a href="">Party Banners</a></li>
</ul>

Can anybody tell me how can I do it? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanduji2986 2016-10-09 17:19
    关注

    This probably does not address your question accurately (not using PHP), but you can consider using jQuery:

    $("li").filter(function(){
        return $(this).find("li").length > 0; // counts children with <li> recursively
    }).addClass("has-sublist");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver