在递增php bootstrapp下拉菜单

时间:2018-05-27 16:37:29

标签: php foreach bootstrap-4 break resume

简而言之:我想输出数组:0,1,2然后中断,回显,输出3,4,5并保持循环。

大家好,我正在使用bootstrap 4和php练习语言的动态php网站上工作。不幸的是,我现在卡住了,因为我不知道如何创建一些类似于算法的东西。还好吧,让我们谈谈代码:

nav.php文件

<ul class="navbar-nav">
<li class='nav-item dropdown'>
  <a class='nav-link dropdown-toggle' data-toggle='dropdown' datatarget='dropdown_target' href='#'>
    <span class='caret'></span>Dropdown
    </a>
    <div class="dropdown-divider"></div>
    <div class="dropdown-menu" aria-labelledby="dropdown_target">
      <!-- <a class="dropdown-item">Dropdown</a> -->
      <?php
      $i=0;
      foreach ($dropItems as $item ) {

        echo "<a class='dropdown-item' href=\"$item[slug]\">$item[title] </a>";
        $i++;
        if($i==1) break;
        echo "<a class='dropdown-item'>Dropdown</a>";

      }
       ?>
    </div>
    </li>
</li>


  <?php
    foreach ($navItems as $item ) {
      echo "<li class='nav-item'> <a class='nav-link' href=\"$item[slug]\">$item[title]</a> </li>";
    }


   ?>

</ul>

arrays.php

<?php
  //Navigatie menu items
  $navItems = array(

                array(
                  "slug" => "index.php",
                  "title" => "home"
                ),
                array(
                  "slug" => "contact.php",
                  "title" => "Contact"
                ),
                array(
                  "slug" => "market.php",
                  "title" => "Marketplace"
                ),
              );

              $dropItems = array(

                            array(
                              "slug" => "#",
                              "title" => "Lps"
                            ),
                            array(
                              "slug" => "#",
                              "title" => "Sps"
                            ),
                            array(
                              "slug" => "market.php",
                              "title" => "Marketplace"
                            ),
                          );
 ?>

2 个答案:

答案 0 :(得分:0)

您可以使用array_chunk将数组拆分为三个块,并使用echo "something";嵌套。

在你的代码中看不到这三个回声是如此,我只是做了一个如何做的例子。

$arr = range(1,12); //example array

$chunks = array_chunk($arr, 3);

Foreach($chunks as $chunk){
    Foreach($chunk as $val){
        Echo $val ." ";
    }
    Echo "\nsomething\n";
}

https://wiki.roblox.com/index.php?title=CFrame

答案 1 :(得分:0)

感谢Andreas,我使用他的方法

$ chunks = array_chunk($ dropItems,2);

      Foreach($chunks as $chunk){
          Foreach($chunk as $item){
              echo "<a class='dropdown-item' href=\"$item[slug]\">$item[title] </a>";
          }
          Echo "<div class='dropdown-divider'></div>";
        }