doushenxu7294 2018-10-05 19:11
浏览 45
已采纳

写了一个代码,显示随机照片,但需要在给定时间只显示四个img

So I have a code that is responsive in nature that is supposed to places four (4) photos side by side and shows the photos at random order.

However, because I am just starting out I don't know how to create a code that makes sure there are only four (4) photos shown at any given time???

So based on my code provided below how do I only show four photos at a time. (Even though I want there to be at least eight (8) possible photos that it sorts through.)

CODE USED:

<br>
<br>
<h50><?php echo $this->translate('Be Discovered!'); ?></h50>
and connected. Meet anyone & see everything!</a></p>
<br>

<?php

$pics = [
    "/public/admin/haleyfoxforabout333.jpg",
    "/public/admin/haleyfoxforabout333.jpg",
    "/public/admin/team5aboutG333.jpg",
    "/public/admin/team6about333.jpg",
    "https://www.w3schools.com/w3images/team1.jpg",
    "https://www.w3schools.com/w3images/team2.jpg",
    "https://www.w3schools.com/w3images/team3.jpg",
    "/public/admin/team4_333.jpg"
];

shuffle($pics);
?>

<style>

    h50 {
        padding: 0 30px 8px;
        width: auto;
        font-size: 40px;
        margin: 0 0 10px 0;
        background-color: transparent;
        border: none;
        border-bottom: 1px solid #D2DBE8;
        border-radius: 3px 3px 0 0;
        display: inline-block;
    }

    .headerz {
        text-align: center;
        padding: 32px;
    }

    .rowz {
        display: -ms-flexbox; /* IE10 */
        display: flex;
        -ms-flex-wrap: wrap; /* IE10 */
        flex-wrap: wrap;
        padding: 0 4px;
    }

    /* Create four equal columns that sits next to each other */
    .columnz {
        -ms-flex: 25%; /* IE10 */
        flex: 25%;
        max-width: 25%;
        padding: 0 4px;
    }

    .columnz img {
        margin-top: 8px;
        vertical-align: middle;
    }

    /* RL - makes a two column-layout instead of four columns */
    @media screen and (max-width: 800px) {
        .columnz {
            -ms-flex: 50%;
            flex: 50%;
            max-width: 50%;
        }
    }

    /* RL Takes the two columns stack on top of each other. */
    @media screen and (max-width: 600px) {
        .columnz {
            -ms-flex: 100%;
            flex: 100%;
            max-width: 100%;
        }
    }

</style>

<!-- Photo Grid -->
<div class="rowz">
    <?php
    foreach ($pics as $pic) {
        echo '<div class="columnz">';
        echo '<img src="' . $pic . '" style="width:100%">';
        echo '</div>';
    }
    ?>
</div>
  • 写回答

1条回答 默认 最新

  • dongyou2714 2018-10-05 19:13
    关注

    Use array_rand, it will give your random keys from your array. Second argument defines how many keys do you want:

    $keys = array_rand($pics, 4);
    foreach ($keys as $key) {
        echo $pics[$key];
    }
    

    Or you can slice you shuffled array:

    shuffle($pics);
    $pics = array_slice($pics, 0, 4); // reduces your array to 4 elements
    

    Or you can use a counter and break foreach:

    $counter = 0;
    foreach($pics as $pic){
        if ($counter == 4) {
            break;
        }
    
        echo $pic;
        $counter++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类