Hello. Tell me how to show DIV after 6 elements formed from the code:

<?php if(count($this->items)): ?> <div class="tagItemList"> <?php foreach($this->items as $item): ?> 

so that after the 6th element a div is output and then by default the remaining elements provided by the code functionality. thank

  • Set the counter, increment it when outputting, and if the condition matches, output the element. - Firepro
  • Thanks @Firepro. But there was a problem. Replacing the code with yours and all together looks like this: '<? Php if (count ($ this-> items)):?> <Div class = "tagItemList"> <? Php $ i = 0; foreach ($ this-> items as $ item) {if (++ $ i == 7) {echo "<div> 1 </ div>"; }}?> 'I am getting the syntax error, unexpected' endforeach 'on the line with <? Php endforeach; ?>. What am I doing wrong? - Ivan
  • Sorry, I did as you said: <? Php if (count ($ this-> items)):?> <Div class = "tagItemList"> <? Php $ i = 0; foreach ($ this-> items as $ item): if (++ $ i == 7) {echo "<div> 1 </ div>"; }?> The error disappeared, but div is not added to the source code - Ivan
  • This means that the $ i operator most likely does not reach 7, draw its conclusion in the echo $ i loop, after foreach and see how many times the given operator arrives - Firepro
  • : (I tried to change the code in different ways, as far as I could understand what needs to be done from the previous koment and something in general skidded. with php almost not familiar. sorry ... - Ivan

1 answer 1

 $i = 0; foreach($this->items as $item) { //делаем пост-инкремент и сравниваем значение if (++$i==7) { echo "<div>Я тот блок, который будет выведен после 6 элемента, то есть буду седьмым </div>"; } } 

We talked to you in the comments and you reported that are not familiar with PHP, I will tell in more detail about your problem solution.

Your code essentially looks at the first condition that there are some items in the this-items array (the size of the array is greater than 0), and if so, displays a div with the class: tagItemList, after which the array of $ this-> items starts sequentially .

 <?php if(count($this->items)): ?> //условие <div class="tagItemList"> //вывод элемента <?php foreach($this->items as $item): ?> //перебор массива 

Your script uses an alternative method for defining control structures, which you can read about here . In each case, the main form of the alternative syntax is to change the opening brace to a colon (:), and the closing bracket to endif ;, endwhile ;, endfor ;, endforeach; or endswitch; respectively.

Consequently, your task is to iterate over the elements of the items array and display a div on element 7. And so, for starters, check whether this condition will work for you.

 <?php if(count($this->items)): ?> <div class="tagItemList"> <?php $i = 0; foreach($this->items as $item): $i++; echo "i = ".$i." "; ?> 

The condition will work if you have i = 7 in the code, then below you can insert the code that will output the required div (and thanks to this you can determine which boundary i will be displayed)

  <?php if(count($this->items)): ?> <div class="tagItemList"> <?php $i = 0; foreach($this->items as $item): if (++$i==7): echo "<div>1</div>"; endif; ?> 

This option of checking variables is the easiest, although of course for debugging, use various extensions that help monitor the progress of the program.