Foreach break and continue in PHP

I ran into a problem. I built the foreach in PHP:

  <?php $show = false; ?> <?php foreach ($this->items as $item) : ?> 

But I want to repeat the first 20 points and continue the next 16 elements.

I managed to take a break after 20 points, but I do not get the next 16 elements (starting from number 21).

This is what I have so far:

 <?php $show = false; ?> <?php $i = $item->id = 1; ?> <?php foreach ($this->items as $item) : ?> <?php if(++$i > 20) break; ?> 

If I set $i to '21' , it still displays element 1, etc.

Solution ## Thanks @dhavald

 <?php $show = false; ?> <?php foreach (array_slice ($this->items, 0, 20) as $item) : ?> 

By placing array_slice in foreach, you can control the elements you want to show. So, on the next div I want to show items 21 to 36, the only thing I needed to change was 0 and 20 in, 20, 36

To clarify what I'm looking for, I have three divs where I want to repeat some elements on it. If the user dumps the div on the first, the first 20 elements will appear, and if the user clicks on div2, the next 16 elements will appear.

How can I fix this code to make this happen?

As I forgot to mention, the solutions you brought helped me understand the process, but since I used the component generator for Joomla 3.1, there is one line of code that makes it a little more complicated to call the first 20 and second 16 items.

Right after the foreach loop there is this line of code

 if($item->state == 1 || ($item->state == 0 && JFactory::getUser()>authorise('core.edit.own',' com_ncitycatemus.vraagantwoordtoevoegen.'.$item->id))): $show = true; 

How can I control it than using a loop? Or do you suggest another way?

Thanks in advance! Really helped me understand the cycle!

+4
source share
4 answers

break breaks out of the loop and stops. continue skips the rest of the code in the loop and moves on to the next iteration.

Using break and continue is, in my opinion, the wrong approach. I would just use two loops:

 $keys = array_keys($this->items); $keysLength = count($keys); echo '<div id="div1">'; for ($i = 0; $i < min($keysLength, 20); $i++) { echo $this->items[$keys[$i]]; } echo '</div>'; if ($keysLength >= 20) { echo '<div id="div2">'; for ($i = 20; $i < min($keysLength, 36); $i++) { //36 being 20 + 16 echo $this->items[$keys[$i]]; } echo '</div>'; } 

Note This will work with any number of items in $this->items . If he is under 20, he will simply skip the next 16, and if he is above 36, he will simply ignore the rest.

+7
source

Do not use break; use continue;

+3
source

you can use array_slice to get part of the $ this-> items array and assign it to 3 different variables and iterate over them individually.

 $itemsDiv1 = array_slice($this->items, 0, 20); // gives first 20 items $itemsDiv2 = array_slice($this->items, 20, 16); // gives next 16 items 

etc.

+2
source
 <?php $i = 0; ?> <!-- first group --> <div> <?php foreach ($this->items as $item) : ?> <?php /** * Check if count divided by 20 has no remainder */ if ( (++$i % 20) == 0 ): ?> </div> <!-- start new group --> <div> <?php endif ?> <?php endforeach ?> <!-- end last group --> </div> 
+1
source

Source: https://habr.com/ru/post/1481802/


All Articles