php中图像的随机播放顺序

发布于 2024-11-03 22:24:52 字数 651 浏览 4 评论 0原文

我有以下图像用于旋转木马。每次加载页面时,我都希望它们以不同的顺序排列。我正在考虑使用随机数生成器对数字进行排序,但后来我不确定如何使这些数字仅使用一次。如果这可以在循环中完成并且可以扩展,那就太好了。

请参阅下面的静态代码,除了末尾的数字之外,所有图像的名称都相同,

        <div class="image-entry">
            <img src="/images/carousel-1.jpg" />
        </div>
        <div class="image-entry">
            <img src="/images/carousel-2.jpg" />
        </div>
        <div class="image-entry">
            <img src="/images/carousel-3.jpg" />
        </div>
        <div class="image-entry">
            <img src="/images/carousel-4.jpg" />
        </div>

谢谢!

I have the following images in that I'm using for a carousel. Each time the page loads I want them in in a different order. I was thinking of just ordering the numbers using a random number generator, but then I'm not sure how to make it so that the numbers are used only once. If this could be done in a loop so it's expandable that would be great.

See the static code below, all the images are named the same except for the number at the end

        <div class="image-entry">
            <img src="/images/carousel-1.jpg" />
        </div>
        <div class="image-entry">
            <img src="/images/carousel-2.jpg" />
        </div>
        <div class="image-entry">
            <img src="/images/carousel-3.jpg" />
        </div>
        <div class="image-entry">
            <img src="/images/carousel-4.jpg" />
        </div>

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

捂风挽笑 2024-11-10 22:24:52

有一个函数,shuffle()

$images = array
(
    '/images/carousel-1.jpg',
    '/images/carousel-2.jpg',
    '/images/carousel-3.jpg',
    '/images/carousel-4.jpg',
);

shuffle($images); // the magic

foreach ($images as $image)
{
    echo '<div class="image-entry">';
    echo "\t" . '<img src="' . $image . '" />';
    echo '</div>';
}

There is a function for that, shuffle():

$images = array
(
    '/images/carousel-1.jpg',
    '/images/carousel-2.jpg',
    '/images/carousel-3.jpg',
    '/images/carousel-4.jpg',
);

shuffle($images); // the magic

foreach ($images as $image)
{
    echo '<div class="image-entry">';
    echo "\t" . '<img src="' . $image . '" />';
    echo '</div>';
}
掐死时间 2024-11-10 22:24:52

假设您知道需要多少张图像(比如 4 张),并且该数量以内的所有图像都是有效的并且以 1 开头:

<?php
$images = range(1, 4);
shuffle($images);

foreach ($images as $_) {
   echo <<<HTML
      <div class="image-entry">
         <img src="/images/carousel-$_.jpg" />
      </div>
HTML;
}

Assuming that you know how many images (say 4) you want and that all images up to that number are valid and start with 1:

<?php
$images = range(1, 4);
shuffle($images);

foreach ($images as $_) {
   echo <<<HTML
      <div class="image-entry">
         <img src="/images/carousel-$_.jpg" />
      </div>
HTML;
}
野心澎湃 2024-11-10 22:24:52

创建一个数字/图像名称数组,然后对数组进行打乱,然后输出它,就像这样。

$images[0] = 'car-1.jpg';
$images[1] = 'car-2.jpg';
$images[2] = 'car-3.jpg';

shuffle($images);

foreach($images as $img){
    echo '<img src="'.$img.'" />';
}

您可以调整上述代码以满足您的需求。

Create an array of the numbers/image names and then shuffle the array, and then output it, like so.

$images[0] = 'car-1.jpg';
$images[1] = 'car-2.jpg';
$images[2] = 'car-3.jpg';

shuffle($images);

foreach($images as $img){
    echo '<img src="'.$img.'" />';
}

You can adapt the above code to suit your needs.

久隐师 2024-11-10 22:24:52
<?php
$imageArr = glob( rtrim( $_SERVER['DOCUMENT_ROOT'] , '/' ).'/images/carousel-*.jpg' );
shuffle( $imageArr );
$outPattern = '<div class="image-entry"><img src="%s" /></div>';
foreach( $imageArr as $carImage )
  echo sprintf( $outPattern , $carImage )."\n";

该解决方案的好处是,它将自动检测图像文件夹中名为 carousel-X.jpg 的所有图像,并在轮播中使用它们。

或者甚至:

<?php
$imageArr = glob( rtrim( $_SERVER['DOCUMENT_ROOT'] , '/' ).'/images/carousel-*.jpg' );
shuffle( $imageArr );
if( count( $imageArr ) ){
  echo '<div class="image-entry"><img src="'.implode( '" /></div><div class="image-entry"><img src="' , $imageArr ).'" /></div>';
}else{
  echo '<!-- No Images in Array //-->';
}
<?php
$imageArr = glob( rtrim( $_SERVER['DOCUMENT_ROOT'] , '/' ).'/images/carousel-*.jpg' );
shuffle( $imageArr );
$outPattern = '<div class="image-entry"><img src="%s" /></div>';
foreach( $imageArr as $carImage )
  echo sprintf( $outPattern , $carImage )."\n";

The benefits of this solution being that it will automagically detect all images called carousel-X.jpg in the images folder and use them in the carousel.

Or even:

<?php
$imageArr = glob( rtrim( $_SERVER['DOCUMENT_ROOT'] , '/' ).'/images/carousel-*.jpg' );
shuffle( $imageArr );
if( count( $imageArr ) ){
  echo '<div class="image-entry"><img src="'.implode( '" /></div><div class="image-entry"><img src="' , $imageArr ).'" /></div>';
}else{
  echo '<!-- No Images in Array //-->';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文