Reputation: 95
I have a loop that creates divs with some content from a database. I have a variable $current_count
which I'm starting at value '0
' which is the first iteration of my loop.
I am using:
if ($current_count == 0 || $current_count % 3 == 0) { echo '<div class="parent">'; }
To create a parent div at the very top of the loop, then again on every iteration divisible by 3. It looks like this (with numbers representing iteration):
0 <div class="parent">
0 <div class="child"></div>
1 <div class="child"></div>
2 <div class="child"></div>
3 <div class="parent">
3 <div class="child"></div>
4 <div class="child"></div>
5 <div class="child"></div>
But the problem is I can't work out how to close those divs, as they would close on different iterations. For example, the parent opened on iteration 0
would need to be closed at the end of iteration 2
.
I need to basically say (pseudo-code):
IF $current_count is equal to (division of 3, minus 1) { etc }
I have tried:
if ($current_count % 3 == (0 - 1)) {}
if ($current_count % (3 == 0) - 1) {}
if ($current_count % 3 == 0 - 1) {}
But none of these are returning true. Does anyone know a way I can do this?
Cheers, Lee.
UPDATE 1: Here's an example of the PHP code currently, to better explain what I'm trying to accomplish:
$current_count = '0';
$ret = '';
foreach ( $brands as $index => $brand ) :
if ($current_count == 0 || $current_count % 3 == 0) {
$ret.= '<div class="parent">'; //Start parent
}
$ret.= '<div class="child"></div>'; //Child
if ($current_count % 3 == (0 - 1)) { // IF LINE 2, 5, 8, 11 etc, NOT WORKING
$ret.= '</div>'; // End the parent
}
$current_count++;
endforeach;
Upvotes: 0
Views: 750
Reputation: 1
if you do it like that it still divided by 3 and not solving the question.
it should be :
if( $key % 3 == 2 ){
</div>
}
Upvotes: 0
Reputation: 11987
try this,
for($i = 0; $i <= 10; $i++) {
if($i % 3 == 0 && $i > 0)// $i > 0 condition because. 0 % 3 is equal to 0 only.
echo $i - 1;// will echo 2,5,8
echo "</div>";// in your case.
}
Upvotes: 2