Dario
Dario

Reputation: 714

Break and Create new <ul> after 10 values from foreach loop php

Im looking to create a 2 or more ul class with li content from a foreach loop if the values count more than 5.

$data = array('Barcelona','Jujuy','Cordoba','Mendoza','Galicia','Madrid','Estonia','New York');

echo '<ul class="row1">';
foreach($data as $value){

   echo '<li>' . $value . '</li>';

}
echo '</ul>'

the result will be:

<ul class="row1">
  <li>Barcelona</li>
  ...
  ...
</ul>

What i want is after 5 towns create a new ul class like

<ul class="row1">
  <li>Barcelona</li>
  ...
  ...
  ...
  ...
</ul>

<ul class="row2">
  <li>Madrid</li>
  ...
  ...
  ...
  ...
</ul>

Is there a way to do this?

Any help is appreciated.

Upvotes: 2

Views: 4073

Answers (5)

dlhines
dlhines

Reputation: 61

I know this is a bit of an older post but I thought I would contribute. I have a wp project where I had to display lot of title / permalink lists throughout the site.

Sverri M. Olsen's answer really made the job much quicker, easier to read, and universal...

function display_title_link_lists($args, $chunk_size) {
$query = new WP_Query( $args );
$title_link = [];

while ( $query->have_posts() ) : $query->the_post();
    $title_link[get_the_title()] = get_the_permalink();
endwhile;

$output_title_link = array_chunk($title_link, $chunk_size, true);
foreach ($output_title_link as $tierOne) {
    echo "<div class='col-sm col-md'><ul>";
    foreach($tierOne as $key => $value) {
        echo "<li><a href='$value'>$key</a></li>";
    }
    echo "</ul></div>";
}

wp_reset_postdata();

I was able to modify it and use it as another utility in other projects.

Upvotes: 0

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

Keep it simple. The array_chunk() function sounds exactly what you are looking for.

In this example there will be 3 items in each list.

$lists  = array_chunk($data, 3);
$number = 1;
foreach ($lists as $items) {
    echo '<ul class="row', $number++, '">';
    foreach ($items as $item) {
        echo '<li>', $item, '</li>';
    }
    echo '</ul>';
}

You can also do it a little more surgically (which probably will consume quite a lot less memory). If you know how many items you have, A, and how many items should fit in a list, B, then you can figure out how many lists you would need, ceil(A / B):

$itemsCount   = count($data);
$itemsPerList = 3;
$listsNeeded  = ceil($itemsCount / $itemsPerList);

for ($i = 0; $i < $listsNeeded; $i++) {
    echo '<ul class="row', ($i + 1), '">';
    for ($j = 0; $j < $itemsPerList; $j++) {
        $index = (($i * $itemsPerList) + $j);
        if (isset($data[$index])) {
            echo '<li>', $data[$index], '</li>';
        } else {
            break;
        }
    }
    echo '</ul>';
}

Upvotes: 3

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

Does it helps ?

$data = array('Barcelona','Jujuy','Cordoba','Mendoza','Galicia','Madrid','Estonia','New York');
$count = 1 ;
$len = sizeof($data);
$row = 1;
foreach($data as $value){
   if($count == 1 ){
    echo '<ul class="row'.$row.'">';
    $row++;
   }
   echo '<li>' . $value . '</li>';
   if($count !=1 && $count%5 == 0){
    echo '</ul>';
    if($len-1 != $count){
        echo '<ul class="row'.$row.'">';
        $row++;
    }
   }
   $count++;

}

Upvotes: 0

Kelly Elton
Kelly Elton

Reputation: 4427

<?php
$data = array('Barcelona','Jujuy','Cordoba','Mendoza','Galicia','Madrid','Estonia','New York');

$cur = 0;
$rowNum = 1;
foreach($data as $value){
    if($cur == 0)
    {
        echo '<ul class="row' . $rowNum . '">';
    }
    echo '    <li>' . $value . '</li>';
    if($cur == 4)
    {
        echo '</ul>';
        $cur = 0;
        $rowNum++;
    }
    else
    {
        $cur++;
    }
}
?>

Upvotes: 8

kero
kero

Reputation: 10638

You can do this by counting how many you did already. This can be achieved by either get a variable and increment it inside the foreach() or directly use a for().

$amount = count($data);
$no_of_ul = 1;
for ($i=0; $i<$amount; $i++) {
    if ($i % 5 == 0) {
        if ($i > 0) {
            echo '</ul>';
        }
        echo '<ul class="row'.$no_of_ul.'">';
        $no_of_ul++;
    }
    echo '<li>'.$data[$i].'</li>';
}

$i % 5 == 0 means "if $i divided by 5 has the rest 0" which is true for $i = 0, 5, 10, .... With this you'll only need to close the last <ul> manually.

Upvotes: 0

Related Questions