Maasha Theytaz
Maasha Theytaz

Reputation: 79

How to target the N first iterations of a while loop?

I'm displaying images on my site. The first 18 images should be written like this:

<img class="item lazy" data-src="<?php echo $path; ?>" src="<?php echo $path; ?>" />

And the rest of them (from the 19th) should be written like that:

<img class="item lazy" data-src="<?php echo $path; ?>" />

I used a while loop but it didn't work (it would show each item displayed within the loop 18 times):

while (...) { // while loop to display items from the database 
    $itemCount = 0;
    while($itemCount <= 18) {
        // show items ($itemId)
    }
}

I can't think of anything... any suggestions?

Upvotes: 0

Views: 125

Answers (1)

abe1432181
abe1432181

Reputation: 1316

<?php
$n=0;
$dir="C:/Chose a folder";
$url="http://....";
$files=scandir($dir);
foreach($files as $filenm) {
    if( $n++<18 ) echo "<img class='item lazy' data-src='$url/$filenm' src='$url/$filenm' />";
    else echo "<img class='item lazy' data-src='$url/$filenm/>";
}
?>

Note, the src should be the URL of the file, not the local filename

Upvotes: 2

Related Questions