0

希望这对某人来说非常简单,但是我的 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

4 回答 4

1

首先,您应该只在数组中存储 url。

其次,您应该在 Next 链接中将下一个视频的 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>
于 2012-08-17T14:53:32.807 回答
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 的参数调用脚本,它将选择该参数作为“随机”数字。

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

于 2012-08-17T14:47:38.020 回答
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; }
 ........
于 2012-08-17T14:46:27.987 回答
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>";
于 2012-08-17T14:59:10.480 回答