Check foreach statement values

2019-09-20 11:55发布

I have a code like this.

    <ul>
    <?php foreach (getUserMainMenus($get_user_id) as $get_main_menu): ?>
    <li class='has-sub'><a href='#'><span><?=$get_main_menu['menu_name']; ?></span></a>
    <ul>
    <?php foreach (getUserChildMenu($get_main_menu['m_id'], $get_user_id) as $sub_menu): ?>
    <li class='has-sub'><a href='<?=$sub_menu['menu_url']; ?>'><span><?=$sub_menu['menu_name']; ?></span></a>

    <ul>
    <?php foreach (getUserSubChildMenu($sub_menu['m_id'], $get_user_id) as $sub_sub_menu): ?>
    <li class='has-sub'><a href='<?=$sub_sub_menu['menu_url']; ?>'><span><?=$sub_sub_menu['menu_name']; ?></span></a></li>
    <?php endforeach; ?>   
    </ul>
    </li>
    <?php endforeach; ?>   
    </ul>
    </li>
    <?php endforeach; ?>   
    </ul>

And the result set for the second foreach is like this.

    Array ( [m_id] => 9 [menu_name] => MenuName [parent_menu_id] => 4 [menu_order] => 1 [menu_url] => menu1.php [status] => 1 )

How to check a if condition after all foreach statement. In other words, i need to get the $sub_menu values and if there is values show the li tag. Also - I need to check the $sub_sub_menu variable, and if there is values show the li tag.

How can i achieve that?

Thanks, Kimz

1条回答
一夜七次
2楼-- · 2019-09-20 12:09

I guess you try to create a navigation menu. Where the sub entries should only appear when the top menu item is selected by the visitor of your page.

Is that right?

Ok if so. You might have in mind that.

  1. if a user displays your page first. you might show only the top menu items.
  2. if then a user selects one of the top menu items he/she clicks on a link an that reloads your script with some additional information.
  3. Now your script needs to figure out which top menu item the user selected based on the additional information.
  4. Depending on the selection of the user you might show or hide submenu items.

What your job here is, you have to make sure that your script detects which top-menu item is clicked at.

Do you need more help, or is it clear what to do?

Ok how about this as an basic example for dynamic php menus as test.php

<?php

  $menu="";

  extract( $_GET, EXTR_PREFIX_ALL, "url" );
  if (isset($url_menu)){
    $menu=$url_menu;
    echo "you selected ".$menu."<br>";
  }     



  echo "<ul>";

  // top menu 1
  echo '<li><a href="./test.php?menu=top1">Top1</a>';
    if ($menu=="top1"){
      echo "<ul>";
        echo "<li>Submenu</li>";
      echo "</ul>";
    }
    echo "</li>";

  // top menu 2
  echo '<li><a href="./test.php?menu=top2">Top2</a>';
    if ($menu=="top2"){
      echo "<ul>";
        echo "<li>Submenu</li>";
      echo "</ul>";
    }
    echo "</li>";

  echo "</ul>";


?>

See any top menu item hands over the additional variable "menu". This is either "top1" or "top2" in this case. Now your script on reload checks whether "menu" is already set and depending on the value of "menu" it shows the corresponding sub menu.

There is still a long way to go, because in my case I use fixed menu items where in your case you load the menu items depending on the "userid".

Let me know if the example above works at your place and if you need additional support to adopt it to your dynamically loaded menus.

Following that idea you need to replace

<li class='has-sub'><a href='#'><span><?=$get_main_menu['menu_name']; ?></span></a>

by adding for example the variable name "level0"

<li class='has-sub'><a href='<?= ?level0=$sub_menu['menu_name']; ?>'><span><? $get_main_menu['menu_name']; ?></span></a>

then you can check in you sub menu if "level0" is set as you expect it and then show or hide the sub menu items.

查看更多
登录 后发表回答