Cameron Lewis
Cameron Lewis

Reputation: 3

PHP Foreach Skip Results from Array

I'm trying to limit the amount of results that show in my foreach, I've got this so far:

    <?php $facilitiescounter = 0; ?>
    <?php foreach ($facilities as $data) { 
    if (++$facilitiescounter == 7) break; 
    echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; }?> 

    <a class="read-more-show hide" href="#">Show More</a> <span class="read-more-content">Show all other results from array <a class="read-more-hide hide" href="#">Show Less</a></span>

I have managed to limit it to the first 6 results, is it possible to do another foreach to get all the other results excluding the first six?

Or is there a better way of doing this? Thanks!

Upvotes: 0

Views: 160

Answers (4)

hlscalon
hlscalon

Reputation: 7552

A simple for loop is more appropriate in this case, as we can determine begin and end easily

To the first six results:

for($i = 0, $t = min(6, count($facilities)); $i < $t; ++$i)
{
    $facilities[$i]->Name
    ....

The other ones:

for($i = 6, $t = count($facilities); $i < $t; ++$i)
{
    $facilities[$i]->Name
    ....

Note: added minimum verification in case $facilities has less than 6 elements. Thanks to @lanis

Upvotes: 3

nerdyDev
nerdyDev

Reputation: 376

<?php $facilitiescounter = 0; ?>
<?php foreach ($facilities as $data) { 
if (++$facilitiescounter <= 7) break; 
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; }?> 

<a class="read-more-show hide" href="#">Show More</a> <span class="read-more-content">Show all other results from array <a class="read-more-hide hide" href="#">Show Less</a></span>

i have changed the if condition only. it will give you proper result. try it!

Upvotes: 0

Ianis
Ianis

Reputation: 1175

You can use array_slice() http://php.net/manual/fr/function.array-slice.php

$datas = array_slice($facilities, 0, 6); // First 6 items

foreach($datas as $data) {
    echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; 
}

$datas = array_slice($facilities, 6); // Items after 6

foreach($datas as $data) {
    echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>'; 
}

Upvotes: 3

Keyur Mistry
Keyur Mistry

Reputation: 926

Yes, you can do it as a better way. Just get your $facilities array in some temporary array which is called $temp and then whenever your foreach loop is executing just remove that element from the $temp array. After that you just replace $facilities array with the $temp.

Upvotes: 0

Related Questions