Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8324591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:07:47+00:00 2026-06-09T00:07:47+00:00

I have a problem with my foreach which returns an extra empty element in

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:07:49+00:00Added an answer on June 9, 2026 at 12:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem with my code. foreach (DataRow dr in dt_pattern.Rows) { part
I have a following problem. I have a ListView which returns data from SQL
I currently have some code which grabs some JSON from a site. This is
I have a problem with a continue statement in my C# Foreach loop. I
I have this problem: $id is id for each user $img = 'http://www.somesite.com/pictures/'; $no_img
I have problem with my query on C, I’m using the oci8 driver. This
Im using the following code: {foreach from=$entries key=i item=topic} {if $topic.topic_style == problem} <li>
I have a problem which I don't understand and there doesn't seem to be
I have a problem trying to get the count out of the following query:
I have an SQLite question which essentially boils down to the following problem. id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.