PHP 随机图像集

发布于 2024-12-18 05:06:16 字数 508 浏览 2 评论 0原文

我正在使用 php 脚本随机显示图像。我已经重复这个脚本三次,因为我想一次显示三个随机图像 - 我不确定如何更改 php 代码以显示 3 个图像。

问题是,我不想遇到所有三个脚本同时显示相同图像的情况。我可以在这段代码中添加一些内容来确保显示的每个图像始终不同吗?

<?php 
$random = "random.txt"; 
$fp = file($random); 
srand((double)microtime()*1000000); 
$rl = $fp[array_rand($fp)]; 
echo $rl; 
?>

html:

 <?php include("rotate.php"); ?>
 <?php include("rotate.php"); ?>
 <?php include("rotate.php"); ?>

*random.txt 只有一个带有链接的文件名列表。

I'm using a php script to randomly show images. I've duplicated this script three times because I wanted to show three random images at once - I'm unsure of how to change the php code to show 3 images.

The problem is, I don't want to run into the chance of all three scripts showing the same images at once. Is there something that I could add to this code to make sure that each image displayed is always different?

<?php 
$random = "random.txt"; 
$fp = file($random); 
srand((double)microtime()*1000000); 
$rl = $fp[array_rand($fp)]; 
echo $rl; 
?>

the html:

 <?php include("rotate.php"); ?>
 <?php include("rotate.php"); ?>
 <?php include("rotate.php"); ?>

*the random.txt just has a list of filenames with links.

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

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

发布评论

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

评论(4

似梦非梦 2024-12-25 05:06:16

简单的解决方案...

  1. 获取随机图像数组(您已经这样做了)
  2. 随机播放数组
  3. 时,将图像从数组末尾弹出

需要一个rotate.php

$random = "random.txt"; 
$fp = file($random); 
shuffle($fb); //randomize the images

每当代码中

<?php include('rotate.php') ?>

每当您需要图像时

<?php echo array_pop( $fb ) ?>

http://php.net/manual/en/function.array-pop.php

Simple solution...

  1. Get the array of random images (you already did this)
  2. Shuffle the array
  3. Pop an image off the end of the array whenever you need one

rotate.php

$random = "random.txt"; 
$fp = file($random); 
shuffle($fb); //randomize the images

in your code

<?php include('rotate.php') ?>

Whenever you need an image

<?php echo array_pop( $fb ) ?>

http://php.net/manual/en/function.array-pop.php

野生奥特曼 2024-12-25 05:06:16
function GetRandomItems($arr, $count)
{
    $result = array();
    $rcount = 0;
    $arrsize = sizeof($arr);
    for ($i = 0; ($i < $count) && ($i < $arrsize); $i++) {
        $idx = mt_rand($rcount, $arrsize);
        $result[$rcount] = trim($arr[$idx]);
        $arr[$idx] = $arr[$rcount];
        $rcount++;
    }
    return $result;
}

$listname = "random.txt";
$list = file($listname);
$random = GetRandomItems($list, 3);
echo implode("<BR>", $list);

PS实际上,盖伦的答案更好。由于某种原因我忘记了随机播放 xD

function GetRandomItems($arr, $count)
{
    $result = array();
    $rcount = 0;
    $arrsize = sizeof($arr);
    for ($i = 0; ($i < $count) && ($i < $arrsize); $i++) {
        $idx = mt_rand($rcount, $arrsize);
        $result[$rcount] = trim($arr[$idx]);
        $arr[$idx] = $arr[$rcount];
        $rcount++;
    }
    return $result;
}

$listname = "random.txt";
$list = file($listname);
$random = GetRandomItems($list, 3);
echo implode("<BR>", $list);

P.S. Actually, Galen's answer is better. For some reason I forgot about shuffle xD

滥情哥ㄟ 2024-12-25 05:06:16

您可以使用 array_rand() 一次选择多个随机键,如下所示:

$random = "random.txt";
$fp = file($random);
shuffle($fp);
// You don't need this. The array_rand() function
// is automatically seeded as of 4.2.0
// srand((double)microtime()*1000000);
$keys = array_rand($fp, 3);
for ($i = 0; $i < 3; $i++):
    $rl = $fp[$keys[$i]]; 
    echo $rl;
endfor;

这样就无需多次包含该文件。这一切都可以一次性完成。

You can use array_rand() to select more than one random key at a time, like this:

$random = "random.txt";
$fp = file($random);
shuffle($fp);
// You don't need this. The array_rand() function
// is automatically seeded as of 4.2.0
// srand((double)microtime()*1000000);
$keys = array_rand($fp, 3);
for ($i = 0; $i < 3; $i++):
    $rl = $fp[$keys[$i]]; 
    echo $rl;
endfor;

This would eliminate the need for including the file multiple times. It can all be done at once.

傲世九天 2024-12-25 05:06:16

您可以编写一个递归函数来检查数组 ID 是否已打印,如果已打印,则再次调用自身。只需将其放入 for 循环中即可打印三次:)

但请记住,真正的随机图像可能会重叠!

$beenDisplayed = array();

function dispRand($id) {
    if (in_array($id, $beenDisplayed)) {
        //generate random number
        dispRand($id);
    }
    else {
        array_push($beenDisplayed, $id);
    }
}

for ($i = 0; $i < 3; $i++) {
    dispRand($random_id);
}

You could write a recursive function to check if the array ID has already been printed, and if it has, call itself again. Just put that in a for loop to print three times :)

Though keep in mind that truly random images could overlap!

$beenDisplayed = array();

function dispRand($id) {
    if (in_array($id, $beenDisplayed)) {
        //generate random number
        dispRand($id);
    }
    else {
        array_push($beenDisplayed, $id);
    }
}

for ($i = 0; $i < 3; $i++) {
    dispRand($random_id);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文