Tell me how to put the correct nesting in the menu. I get a sequence of elements

<ul> <li><a>text1</a></li> </ul> <ul> <li><a>text2</a></li> </ul> 

and I need such a sequence

 <ul> <li><a>text1</a></li> <ul> <li><a>text2</a></li> <li><a>text3</a></li> </ul> </ul> 

need to change the condition in the code, help fix

 <?if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?> <? //var_dump($arResult); //echo "<pre>";print_r($arResult);echo "</pre>";?> <div id="t_menu"> <?for($i=0; $i < count($arResult); $i++) { if($arResult[$i]["DEPTH_LEVEL"] == 1) { //echo'<pre>';var_dump($arResult[$i]["SELECTED"]); echo'</pre>'; ?><div class="itm"> <?if($arResult[$i]["SELECTED"]) {?><div class="itm_act select"><?} else {?><div class="itm_act"><?}?> <a class="vinetka" href="<?=$arResult[$i]["LINK"]?>"><?=$arResult[$i]["TEXT"]?></a> </div> <?if($arResult[$i+1]["DEPTH_LEVEL"] > 1 || $arResult[$i+1]["PARAMS"]["DEPTH_LEVEL"] > 2) {?><ul class="sub_top_men"> <?while($arResult[$i+1]["DEPTH_LEVEL"] > 1 ) {?> <?if( $arResult[$i+1]["PARAMS"]["DEPTH_LEVEL"] > 2) {?><ul class="sub_top_men3"> <?}?> <li><a class="s_itm" href="<?=$arResult[$i+1]["LINK"]?>"><?=$arResult[$i+1]["TEXT"]?></a></li> <?if( $arResult[$i+1]["PARAMS"]["DEPTH_LEVEL"] > 2) {?></ul> <?}?> <? $i++; }?> </ul> <?}?> </div> <?if($i+1 < count($arResult)) {?> <span class="line">|</span> <?}?> <?} }?> <a class="link-rss" href="/press/subscribe.php">&nbsp;</a> <br class="clearfloat" /> </div> <div class="clearfloat"></div> 

    1 answer 1

    You have to change this code section.

     <?while($arResult[$i+1]["DEPTH_LEVEL"] > 1 ) {?> <?if( $arResult[$i+1]["PARAMS"]["DEPTH_LEVEL"] > 2) {?><ul class="sub_top_men3"> <?}?> <li><a class="s_itm" href="<?=$arResult[$i+1]["LINK"]?>"><?=$arResult[$i+1]["TEXT"]?></a></li> <?if( $arResult[$i+1]["PARAMS"]["DEPTH_LEVEL"] > 2) {?></ul> <?}?> <? $i++; }?> 

    Here, the previous depth is not checked and therefore a submenu is displayed on each line, as it is closed in each line, instead of the submenu being printed exactly with increasing depth, and closed only with decreasing depth. As an option like this:

     if($arr[i+1]['params']['deph'] > $arr[i]['params']['deph']) { echo '<ul class="sub_top_menu3">'; } echo '<li><a class="s_itm" .... </a></li>'; i++; if($arr[i+1]['params']['deph'] < $arr[i]['params']['deph']) { echo '</ul>'; } 

    There are options using variables or another way to control the change in nesting.

    • ideone.com/brk2Ep something does not work ((( - ChromeChrome
    • And what happens? - Dmitriy
    • can you show the code? - Dmitriy
    • one
      Look at my code, you need to raise above i ++ - Dmitry
    • 2
      Was glad to help. - Dmitry