0

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!

2
  • what about a simple for-loop? Commented Jan 20, 2016 at 12:05
  • Maybe try use for ? Commented Jan 20, 2016 at 12:09

4 Answers 4

3

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

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

1 Comment

This answer is great... except $facilities has less than 6 items... so I would use min(6, count($facilities) in the for continue condition : for($i = 0; $i < min(6, count($facilities)); ++$i)
3

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>'; 
}

Comments

0

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.

Comments

0
<?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!

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.