Jump to content

nested foreach loops break html nesting


berechar
 Share

Recommended Posts

Hello everyone,

Until now I haven't had any problems with the great processwire cms, but this is something quite strange ( or the case is that I'm doing something terribly wrong...)

On basic-page.php i run the following code:

foreach($page->children as $child){
   echo '<li class="A">';
   foreach($child->children as $book){
       echo '<li class="B">';
       echo '</li>';
   }
   echo '</li>';
}

with this as my expectations:

<li class="A">

     <li class="B"></li>

     <li class="B"></li>

</li>

<li class="A">

     <li class="B"></li>

     <li class="B"></li>

</li>

however, the following was returned:

<li class="A"></li>

<li class="B"></li>

<li class="B"></li>

<li class="A"></li>

<li class="B"></li>

<li class="B"></li>

what is going here... any help is much appreciated :-)

Link to comment
Share on other sites

It's not a bug, it's how list elements work. Now the browser tries to make something of it.

list elements need an <ul> or for numbered <ol>

echo '<ul>';
foreach($page->children as $child){
   echo '<li class="A">';
   echo '<ul>';
   foreach($child->children as $book){
       echo '<li class="B">';
       echo '</li>';
   }
   echo '</ul>';
   echo '</li>';
}
echo '</ul>';

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...