检查foreach语句值 [英] Check foreach statement values

查看:180
本文介绍了检查foreach语句值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码。

    <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>

第二个foreach的结果集是这样的。

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 )

如何检查所有foreach之后的条件声明。
换句话说,我需要获取$ sub_menu值,如果有值显示li标签。
另外 - 我需要检查$ sub_sub_menu变量,如果有值显示li标签。

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.

如何实现?

谢谢,
Kimz

Thanks, Kimz

推荐答案

我想你尝试创建一个导航菜单。只有当您的页面的访问者选择顶级菜单项时,才会显示子条目。

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.

是吗?

你可能记住,如果用户首先显示您的页面,那么您可能会想到。

Ok if so. You might have in mind that.


  1. 您可能仅显示顶级菜单项。

  2. 如果用户选择了他/她在链接上点击的顶级菜单项,该链接会重新加载脚本并附加一些附加信息。

  3. 现在您的脚本需要根据附加信息找出用户选择的顶级菜单项。

  4. 根据用户的选择,您可能会显示或隐藏子菜单项。

  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?

动态php菜单的基本示例为 test.php

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>";


?>

查看任何顶级菜单项,再次显示附加变量菜单。在这种情况下,这是top1或top2。现在,您的脚本在重新加载时检查是否已经设置了菜单,并根据菜单的值显示相应的子菜单。

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.

还有很长的路要走去,因为在我的情况下,我使用固定的菜单项,在你的情况下,您加载菜单项取决于userid。

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>

通过添加例如变量名level0

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>

那么你可以检查你的子菜单,如果level0设置为你期望的,然后显示或隐藏子菜单项。

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.

这篇关于检查foreach语句值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆