带有上一个/下一个的PHP随机数组

时间:2012-08-17 14:41:14

标签: php arrays random

希望对于有人帮助的人来说这很简单,但我的PHP非常有限,所以这里有:

我有一个随机阵列设置,在每次刷新页面时输入不同的视频,我设置了一个“下一个”链接,如果按下该链接,则刷新数组并输出“下一个”视频。事实是,它不是数组中的“下一个”视频,因为它通过随机函数传递,只输出找到的任何一个。在大多数情况下,由于阵列中只有四个视频,因此视频相同。

每次访问页面时,它都需要是一个随机视频,但如果按下“下一步”,则需要在循环中继续在数组中继续。

到目前为止,这是我的代码:

<div class="video-container">
            <?php
                $randomNumber = rand(0,4);
                $videoArray = array(
                    '<iframe src="http://player.vimeo.com/video/46808655?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
                    '<iframe src="http://player.vimeo.com/video/46803192?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
                    '<iframe src="http://player.vimeo.com/video/46811051?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
                    '<iframe src="http://player.vimeo.com/video/46817110?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
                    '<iframe src="http://player.vimeo.com/video/46822673?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
                );
                echo $videoArray[$randomNumber];
                $current_index = array_search($randomNumber, $videoArray);
                $next = $current_index - +1;        
        ?>
    </div>
    <?php if ($videoArray > 0): ?>
        <a href="<?= $videoArray[$next] ?>">Next</a>
    <?php endif; ?>

任何帮助都会受到赞赏:)

4 个答案:

答案 0 :(得分:1)

您必须设置$_GET参数

<a href="play_video.php?v=<?=$next ?>">Next</a>

randomNumber

<?php

     $videoArray = array(/*  */);

     if(isset($_GET['v']) && (int)$_GET['v'] < count($videoArray)){
         $randomNumber = (int)$_GET['v'];
     } else {
         $randomNumber = rand(0,4);
     }
     $current_index = $videoArray[$randomNumber];
     $next = $current_index+1;
     if($next >= count($videoArray)){ $next = 0; }
 ........

答案 1 :(得分:1)

这样的事情怎么样:

<div class="video-container">
        <?php

            if (isset($_REQUEST["video"])) {
                $randomNumber = $_REQUEST["video"];
            } else {
                $randomNumber = rand(0,4);
            }

            $videoArray = array(
                '<iframe src="http://player.vimeo.com/video/46808655?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
                '<iframe src="http://player.vimeo.com/video/46803192?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
                '<iframe src="http://player.vimeo.com/video/46811051?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
                '<iframe src="http://player.vimeo.com/video/46817110?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
                '<iframe src="http://player.vimeo.com/video/46822673?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
            );
            echo $videoArray[$randomNumber];
            $current_index = array_search($randomNumber, $videoArray);
            $next = $current_index - +1;        
    ?>
</div>
<?php if ($videoArray > 0): ?>
    <a href="<?php echo $_SERVER["PHP_SELF"] ?>?video=<?= $videoArray[$next] ?>">Next</a>
<?php endif; ?>

如果使用名为video的参数调用脚本,则会将其选为“随机”数字。

还有一些其他调整可以添加,例如确保所选视频存在,以及考虑如果有人在最​​终视频上点击下一步该怎么办。

答案 2 :(得分:1)

首先,您应该只在网址中存储网址。

其次,您应该在下一个链接中将下一个视频的ID作为url参数传递。

<div class="video-container">
    <?php
            // Array with video urls
    $videoArray = array(
        'http://player.vimeo.com/video/46808655?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff',
        'http://player.vimeo.com/video/46803192?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff',
        'http://player.vimeo.com/video/46811051?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff',
        'http://player.vimeo.com/video/46817110?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff',
        'http://player.vimeo.com/video/46822673?title=0&amp;portrait=0&amp;byline=0&amp;color=ffffff'
        );

            // Verify if "videoid" has been passed and it is valid
    if (isset($_GET["videoid"])
        && is_numeric($_GET["videoid"])
        && ($_GET["videoid"] >= 0)
        && ($_GET["videoid"] < count($videoArray))
    {
        // videoid is valid, use it
        $videoid = $_GET["videoid"];
    }
    else
    {
        // videoid is invalid or not set, generate random videoid
        $videoid = rand(0, count($videoArray) - 1);
    }
    ?>
    <iframe src="<?php print($videoArray[$videoid]); ?>" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen>
    </iframe>
    <?php
    // calculate id for next video with overflow
    $nextid = $videoid + 1;
    if ($nextid >= count($videoArray))
        $nextid = 0;
    ?>
</div>
<a href="?videoid=<?php print($nextid); ?>">Next</a>

答案 3 :(得分:0)

在输出任何脚本之前,请键入: session_start(); 然后做

if(!isset($_SESSION['curVid'])) { 
  $_SESSION['curVid'] = 0;
  $_SESSION['videos'] = $videoArray;
  shuffle($_SESSION['videos']);
}
if(isset($_GET['next'])) { 
  $current = intval($_GET['next']);
  if($current > count($_SESSION['videos'])) { 
    $current = 0;
  }
  $_SESSION['curVid'] = $current;
}
$current = $_SESSION['curVid'];
$video = $_SESSION['videos'][$current];
$next = $_SESSION['curVid']+1;

echo "<a href=\"./?next={$next}\">Next video</a>";