0

I have an array with many values, I want to achieve a list like this with PHP.

<div class="item">
  <div class="thumb-cont">
     <ul>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
     </ul>
    </div>
</div>
<div class="item">
  <div class="thumb-cont">
     <ul>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
     </ul>
    </div>
</div>
<div class="item">
  <div class="thumb-cont">
     <ul>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
        <li><a href="#"><img src=""></a></li>
     </ul>
    </div>
</div>

can any body help me how can i manage this list in foreach or for loop in php. Thanks for the help!

3 Answers 3

6

$arr is having 18 elements and $html will have all html string with ul

$len = count($arr);
$html = '<ul><div class="item"><div class="thumb">';
for ($i = 0; $i < $len; $i++) {
    if(!$i%6 && $i != 0){
        $html .= '</ul></div></div><ul><div class="item"><div class="thumb">';
    }

    $html .='<li>content</li>';
}
$html .='</ul></div></div>';

for 18 elements this will contain like

    6 elements
    6 elements
    6 elemnt

Sign up to request clarification or add additional context in comments.

Comments

0

I'm assuming you're generating HTML <ul> tags inside your PHP loop. Use the following code:

for($ind = 1; $ind <= 18; $ind++)
{
    if($ind <= 6) // Loop 1 through 6
    {
        // Add <ul> elements here.
    }

    else if($ind >= 7 && $ind <= 12) // Loop 7 through 12
    {
        // Add <ul> elements here.
    }

    else if($ind >=13 $ind <= 18) // Loop 13 through 18
    {
            // Add <ul> elements here.
    }
}

Please mark it as answer if it solves your problem.

1 Comment

The Answer from developerCK is better, because if you have more that 18 Elements, than in your solution you will have to add an another if-case.
0
$counter = 0;
foreach($arrayElems as $elem) {
    $counter++;
    if ($counter < 6) {
        // build ul of first 6 elements
    }
    else if($counter < 12) {
        // build ul of next 6 elements
    }
    else {
        // build ul of next 6 elements
    }

}

Comments

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.