how do i get images to display in a rows like this using php and css?

前端 未结 3 784
别跟我提以往
别跟我提以往 2021-01-23 11:15

i wanted to know how can i display the images like the on in the picture: i know that i have to retrive the images using a while loop, but then displaying them like this is the

3条回答
  •  无人共我
    2021-01-23 11:45

    Displaying the image is a matter of using the html tag, and you can use the modulo operator % to know when you've displayed X images so you can close the existing row and open a new one.

    $images ; //array of image urls
    $countImages = count($images) ;
    
    $imagesPerRow = 5 ;
    
    for ($i = 0 ; $i < $countImages; $i++) {
        //display image here
        $image = $images[$i] ;
        echo "" ;
    
        if ($i % $imagesPerRow == 0) {
            //have displayed an entire row
            echo '
    ' ; } }

    Modulo returns the remainder of the division. So, lets say you want rows of 5 images. If $i is divided by 5 exactly then the remainder will be 0, which means 5 imageshave been shown so it's time for a new row.

提交回复
热议问题