duanhuanbo5225 2012-07-30 22:52
浏览 46
已采纳

如果索引匹配计数,则Php foreach返回一个额外的空行

I have a problem with my foreach which returns an extra empty element in output if the "if" condition matches the index count.

this is my code

<ul id="toplevel" class="menu">
  <?php 
  foreach($categories as $category){
    $parenturl = 'some url'.$category->category_id;
    $parentname = $category->category_name;
  ?>
    <li>
      <?php echo link($parenturl, $parentname); ?>
      <?php if($category->childs) : ?>
      <ul>
        <li>
         <?php echo link($parenturl, $parentname); ?>
          <ul>
          <?php
          $i=0;
          foreach($category->childs as $child){
            $i++;
            $childurl = 'some url'.$child->category_id;
            $childname = $child->category_name;
          ?>
          <li><?php echo link($childurl, $childname); ?></li>
          <?php
          if($i % 10 == 0) :
          ?>
           </ul></li><li><?php echo link($parenturl, $parentname); ?><ul>
          <?php
          endif;
          }
          ?>
          </ul>
        </li>
      </ul>
      <?php endif; ?>
    </li>
  <?php
  }
  ?>
  </ul>

So if the $child has exactly 10 items than this will output 11 and I don't know why.

The Output looks like this:

<ul id="toplevel" class="menu">
<li>
  <a>Main Category</a>            
  <ul>
    <li>
     <a>Main Category</a>          
     <ul>
        <li><a>Sub 1</a></li>
        <li><a>Sub 2</a></li>
        <li><a>Sub 3</a></li>
        <li><a>Sub 4</a></li>
        <li><a>Sub 5</a></li>
        <li><a>Sub 6</a></li>
        <li><a>Sub 7</a></li>
        <li><a>Sub 8</a></li>
        <li><a>Sub 9</a></li>
        <li><a>Sub 10</a></li>
      </ul>
    </li>
    <li>
    <a>Main Category</a>
    <ul> </ul>
    </li>
  </ul>
</li>
</ul>

I was playing around with the count number but every time when the $child matches the number set it outputs one more. But if $child has less or more items than the count it works fine.

The HTML looks like this because I'm using jQuery Verical Megamenu by Design Chemical and that needs this layout

  • 写回答

1条回答 默认 最新

  • dongyouji7022 2012-07-30 23:05
    关注

    Consider this code snippet:

          <?php
          $i=0;
          foreach($category->childs as $child){
            $i++;
            $childurl = 'some url'.$child->category_id;
            $childname = $child->category_name;
          ?>
    

    The variable $i is defined prior to the foreach loop. Inside the foreach loop, $i is incremented by on on each iteration. After the foreach loop the variable $i will have a value equal to the number of times the foreach loop iterated. Since the foreach loop iterates a number of times equal to the number of elements in $child, $i also equals the current elements in $child the loop is "focused" on.

    Now consider this code snippet below:

          <?php
          if($i % 10 == 0) :
          ?>
           </ul></li><li><?php echo link($parenturl, $parentname); ?><ul>
          <?php
          endif;
          }
          ?>
    

    Here the if statement checks $i to see if it is evenly divisible by 10. It does this with the Modulo operator ( % ) which returns the remainder when the left number is divided by the right. If $i is evenly divisible by 10, the Boolean statement is true and the script adds the contents up to the "endif" statement.

    That is why your code creates an extra line when $child has 10 elements. It would also create an extra line when $child has 20, 30 or any number of elements that is evenly divisible by 10.

    Right now you code produces an extra tag on multiples of ten to accommodate for the next set of ten elements. However the code produces this this tag on multiples of ten regardless of whether there is an addition element in the array after that multiple of ten. This means that if the array has exactly a multiple of ten elements, you receive a "blank/empty" tag.

    Consider this fix:

    <ul id="toplevel" class="menu">
      <?php 
      foreach($categories as $category){
        $parenturl = 'some url'.$category->category_id;
        $parentname = $category->category_name;
      ?>
        <li>
          <?php echo link($parenturl, $parentname); ?>
          <?php if($category->childs) : ?>
          <ul>
            <li>
             <?php echo link($parenturl, $parentname); ?>
              <ul>
              <?php
              $i=0;
              foreach($category->childs as $child){
                $i++;
                $childurl = 'some url'.$child->category_id;
                $childname = $child->category_name;
              ?>
              <li><?php echo link($childurl, $childname); ?></li>
              <?php
              if($i % 10 == 0 && $i < count($category->childs) ) :
              ?>
               </ul></li><li><?php echo link($parenturl, $parentname); ?><ul>
              <?php 
              endif;
              }
              ?> 
              </ul>
            </li>
          </ul>
          <?php endif; ?>
        </li>
      <?php
      }
      ?>
      </ul>
    

    I Changed:

    if($i % 10 == 0)
    

    To:

    if($i % 10 == 0 && $i < count($category->childs) )
    

    EDIT: Revised answer after clarifying the question. The changed conditional statement above will now check to make sure that there are additional elements prior to creating a new tag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?