0

sorry for the very stupid question. It seems to be more of a logical problem than a programming one, but whatever.

So i have this nice array that I want to output in html and I wnt to split it in groups of three elements.

<DIV>
   product1
   product2
   product3
</DIV>
<DIV>
  product4
</DIV>

this is my code

 $counter = 0;

        foreach ($prods as $prod) {
            if($counter == 0 || ($counter % 3) == 0) {
                $htm_l .= '<div data-count="' . $counter . '">';
            }

            $htm_l .= '<br> PROD' . $counter;
            $counter++;

            if($counter == 0 || ($counter % 3) == 0) {
                $htm_l .= '</div>';
            }
        }

Of course it produces wrong results like:

<div data-count="0">
    <br> PROD0
    <br> PROD1
    <br> PROD2
</div>
<div data-count="3">
    <br> PROD3

Missing a closing div. What is a clean way to achieve my result? Thanks in advance

1
  • What is the desired output? Commented Oct 11, 2017 at 10:54

4 Answers 4

1

Use the below code

$html = '';
$new_array = array_chunk($prods, 3);
$i = 0;
foreach ($new_array as $key => $value) {
    $html .= '<div data-count="'.$i.'">';
        foreach ($value as $ele) {
            $html .= '<br>'.$ele;
            $i++;
        }
    $html .= '</div>';
}
Sign up to request clarification or add additional context in comments.

Comments

1

One quick solution move $counter++; after condition. Your complete code will be:

 $counter = 0;
$htm_l = "";

foreach ($prods as $prod) {
    if($counter == 0 || ($counter % 3) == 0) {
        $htm_l .= '<div data-count="' . $counter . '">';
    }

    $htm_l .= '<br> PROD' . $counter;
    if($counter == 0 || ($counter % 3) == 0) {
        $htm_l .= '</div>';
    }
    $counter++;


}

echo $htm_l;

1 Comment

In this case after the first cycle since counter is 0 you close immediately the /div <div data-count="0"> <br> PROD0 <br> </div>. then it works fine, we should check if it's the really first cycle but I think it's not a good solution
0

after $counter=3 it will increment 1 by using $counter++ so now $counter=4

and your condition for check

 if($counter == 0 || ($counter % 3) == 0) {
       $htm_l .= '</div>';
 }

its wrong and no </div> assign so after forloop end you will set $counter=0 and then check

if($counter == 0 || ($counter % 3) == 0) {
       $htm_l .= '</div>';
}

so it will work

<?php
$counter = 0;
$htm_l="";
foreach ($prods as $prod){
    if($counter == 0 || ($counter % 3) == 0) {
        $htm_l .= '<div data-count="' . $counter . '">';
    }

    $htm_l .= '<br> PROD' . $counter;
    $counter++;

    if($counter == 0 || ($counter % 3) == 0) {
        $htm_l .= '</div>';
    }
}
$counter=0;
if($counter == 0 || ($counter % 3) == 0) {
    $htm_l .= '</div>';
}
echo $htm_l;

1 Comment

this works fine, I don't know if it's the cleanest way, but it's a good way
0

You might want to take a look at array_chunk(), which chunks an array into arrays with X elements: https://secure.php.net/manual/en/function.array-chunk.php

$prods = ['Product1', 'Product2', 'Product3', 'Product4'];
foreach (array_chunk($prods, 3) as $chunk) {
    echo '<div>' . PHP_EOL;
    foreach ($chunk as $product) {
        echo '<br>' . $product  . PHP_EOL;
    }
    echo '</div>'  . PHP_EOL;
}

1 Comment

Probably works too, but since in my case $products is an object wich is foreachable, but not strictly an array it doesn't work, since the first argument of array_chunk must be an array. Of course we can fill an array first or do other things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.