简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM