Limit for each loop to 3 results

Hi Everyone,

I have been doing some research to come up with a solution to have my foreach loops stop at 3 results. Here is what I had originally which showed everything in the folder;

<?php

            $dir = "../corq media 3.1/thumbnail/";
            $dh = opendir($dir);
            while($filename = readdir($dh))
            {
                $filepath = $dir.$filename;
                if(is_file($filepath) and ereg("\.jpg" ,$filename))
                {
                    $gallery[] = $filepath;
                }
            }

            sort($gallery);
                foreach($gallery as $image)
            {
                    $search  = array('thumbnail', 'jpg');
                    $replace = array('img', 'php');
                    $link = str_replace($search, $replace, $image);
                    echo "<article class ='imgGallery'><a class ='shrink' href='$link'><img src='$image' height='310' width='355' /></a></article>";
            }

        ?>

What I read online was to use an if statement with a break in it. What I tried was this;

<?php

            $dir = "../corq media 3.1/thumbnail/";
            $dh = opendir($dir);
            while($filename = readdir($dh))
            {
                $filepath = $dir.$filename;
                if(is_file($filepath) and ereg("\.jpg" ,$filename))
                {
                    $gallery[] = $filepath;
                }
            }

            sort($gallery);
                foreach($gallery as $image)
            {
               if($image>3) {
                    $search  = array('thumbnail', 'jpg');
                    $replace = array('img', 'php');
                    $link = str_replace($search, $replace, $image);
                    echo "<article class ='imgGallery'><a class ='shrink' href='$link'><img src='$image' height='310' width='355' /></a></article>";
               }
            else {
                    break;
                   }
            }

        ?>

This wasn’t successful…I also attempted variation of this to see what would happen however the most I go was the images in the folder to not load at all…

Any assistance would be great or maybe point me in the right direct in order to challenge me to find a solution.

Thank you!

[FPHP]array_slice[/FPHP]

1 Like
<?php

$dir = "../corq media 3.1/thumbnail/";
$dh = opendir($dir);
while($filename = readdir($dh))
{
$filepath = $dir.$filename;
if(is_file($filepath) and ereg("\.jpg" ,$filename))
{
$gallery[] = $filepath;
}
}

$homePage = array_slice($gallery, 0, 3);

sort($gallery);
foreach($homePage as $image)
{
$search  = array('thumbnail', 'jpg');
$replace = array('img', 'php');
$link = str_replace($search, $replace, $image);
echo "<article class ='imgGallery'><a class ='shrink' href='$link'><img src='$image' height='310' width='355' /></a></article>";
}

?>

Worked!!!

Thank you!!!

You might want to sort gallery first, then slice, but yup, thats the idea!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.