我怎样才能让这个PHP代码工作?我需要使用数组列表制作视频iframe冲浪者
<?php
$videos = array("UT163ZOeKz0" , "ur47gqA1CQo" , "UPEeggt2qAo" , "6lO07NjTsrM");
$list = array_search($videos);
$id = isset($_GET['id']) ? $_GET['id'] : $list;
echo "<iframe width='375' height='310' src='http://www.youtube.com/embed/".$videos[$id]."?&fmt=22&autoplay=1'> </iframe>";
$next = isset($videos[($id+1)]) ? ($id+1) : 0;
$prev = isset($videos[($id-1)]) ? ($id-1) : count($videos)-1;
?>
<a href="video.php?id=<?php echo $prev; ?>">Previous video</a> | <a href="video.php?id=<?php echo $next; ?>">Next video</a>
如何根据数组列表按顺序显示视频?
我该怎么办?
我将非常感谢您的回答。
答案 0 :(得分:0)
我建议您使用临时存储,就像使用此会话一样。只需保存您当前的当前索引,并将其用于$videos
索引。例如:
session_start();
$videos = array("UT163ZOeKz0" , "ur47gqA1CQo" , "UPEeggt2qAo" , "6lO07NjTsrM");
$size = count($videos) - 1;
// simple initialization
$_SESSION['current'] = !isset($_SESSION['current']) ? 0 : $_SESSION['current'];
if(isset($_GET['next'])) { // if chosen next
// if out of bouds, reset to the first, else shift to next key
$_SESSION['current'] = ($_SESSION['current'] >= $size) ? 0 : $_SESSION['current']+1;
}
if(isset($_GET['prev'])) { // if chosen prev
// if out of bounds show the last, else shift to previous key
$_SESSION['current'] = ($_SESSION['current'] > 0) ? $_SESSION['current']-1 : $size;
}
$video = $videos[$_SESSION['current']]; // assign the current key
?>
<iframe width='375' height='310' src="http://www.youtube.com/embed/<?php echo $video; ?>?&fmt=22&autoplay=1"></iframe><br/>
<a href="index.php?prev">Previous video</a> | <a href="index.php?next">Next video</a>