# Loop using while, better solution?

**URL:** <https://forum.getkirby.com/t/loop-using-while-better-solution/26972>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 8, 2022, 9:14am UTC](https://forum.getkirby.com/t/loop-using-while-better-solution/26972 "2022-11-08T09:14:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 8, 2022, 9:14am UTC](https://forum.getkirby.com/t/loop-using-while-better-solution/26972/1 "2022-11-08T09:14:59Z")

</div>

I’m building a page with two colums : `one` and `two`.  
I would like to populate these columns with a limit of 2 `items` in the first column so the remaining item will fallback to the second column.

I’m trying to avoid being too complicated here and I’m looking for a nice way to do it. Or having the same code for column one and two duplicated…  
I first tested with a while.

```auto
<div class="one">

    <?php foreach ($site->pages()->limit(2) as $item) : ?>
        <li style="color: blue" id="<?= $item->title() ?> "><?= $item->title() ?></li>
    <?php endforeach; ?>

</div>
<div class="two">

    <?php foreach ($site->pages() as $item) : ?>
        <li style="color: blue" id="<?= $item->title() ?> "><?= $item->title() ?></li>
    <?php endforeach; ?>

</div>

```

I thought about something like an `e()` condition to switch the class to `two` but this won’t work until I loop through it.

It’s some kind of logic I haven’t found yet.  
Thanks

---

<div class="post-metadata">

**Author:** ![manuelmoreale](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/manuelmoreale/32/839_2.png) [@manuelmoreale](https://forum.getkirby.com/u/manuelmoreale)\
**Post date:** [November 8, 2022, 10:08am UTC](https://forum.getkirby.com/t/loop-using-while-better-solution/26972/2 "2022-11-08T10:08:22Z")

</div>

It depends what your frontend looks like. I’m already confused as to why you have `<li>` inside a `<div>` for example.

There are CSS only soutions to this kind of problems but it all comes down to the design and how it needs to scale across devices

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 8, 2022, 10:32am UTC](https://forum.getkirby.com/t/loop-using-while-better-solution/26972/3 "2022-11-08T10:32:05Z")

</div>

Thanks,  
Well, the `div` with `li` inside was a little confusion while trying to make this code an example.

I tried to manage that using css grid too, since it can do it, but not really the way I want it.  
The best solution I have found is to get a first `ul` for one and another for two.

What I would like to avoid is a redundance. (Exactly what the example is showing)

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [November 8, 2022, 11:25am UTC](https://forum.getkirby.com/t/loop-using-while-better-solution/26972/4 "2022-11-08T11:25:47Z")

</div>

```auto
<?php
$chunks = [
  'one' => $site->pages()->limit(2);
  'two' => $site->pages()->offset(2);
];

foreach ($chunks as $key => $items): ?>

<ul class="<?= $key ?>">
      <?php foreach ($items as $item) : ?>
              <li style="color: blue" id="<?= $item->title() ?> "><?= $item->title() ?></li>
       <?php endforeach; ?>
</ul>
    
<?php endforeach; ?>

```

---

<div class="post-metadata">

**Author:** ![paubou](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/paubou/32/10761_2.png) [@paubou](https://forum.getkirby.com/u/paubou)\
**Post date:** [November 8, 2022, 11:54am UTC](https://forum.getkirby.com/t/loop-using-while-better-solution/26972/5 "2022-11-08T11:54:56Z")

</div>

Thanks,  
that’s it.  
I fixed the array though:

```auto
$offset = "2";
$chunks = array(
    'one' => $site->pages()->limit($offset),
    'two' => $site->pages()->offset($offset),
);

```
