我制作了一个video.php页面,根据youtube上视频的ID显示来自youtube的视频,所以我做了类似的事情
if($_GET['id']=="jquery11"){
?>
<div id="video" ><iframe width="640" height="360" src="http://www.youtube.com/embed/6PM6t5RFR2w?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>
<?
}
if($_GET['id']=="jquery12"){
?>
<div id="video" ><iframe width="640" height="360" src="http://www.youtube.com/embed/fE0GC7KFIno?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>
<?
}
if($_GET['id']=="jquery13"){
?>
<div id="video" ><iframe width="640" height="360" src="http://www.youtube.com/embed/xuFa2R4c6Gc?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>
<?
}
if($_GET['id']=="jquery14"){
?>
<div id="video" ><iframe width="640" height="360" src="http://www.youtube.com/embed/M971xYWiS7M?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>
<?
}
if($_GET['id']=="jquery15"){
?>
<div id="video" ><iframe width="640" height="360" src="http://www.youtube.com/embed/oZ_J422z4WI?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>
<?
}
if($_GET['id']=="jquery16"){
?>
<div id="video" ><iframe width="640" height="360" src="http://www.youtube.com/embed/tYJMitUfSvs?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>
<?
}
if($_GET['id']=="jquery17"){
?>
<div id="video" ><iframe width="640" height="360" src="http://www.youtube.com/embed/wZPhQzqGzls?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>
<?
}
if($_GET['id']=="jquery18"){
?>
<div id="video" ><iframe width="640" height="360" src="http://www.youtube.com/embed/Cdwn2JoCYQ0?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>
<?
}
if($_GET['id']=="jquery19"){
?>
<div id="video" ><iframe width="640" height="360" src="http://www.youtube.com/embed/cKCiNf23Atg?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>
<?
}
?>
任何改变此方法的想法会减少获取请求吗?我尝试制作关联数组,但我认为它是相同的
答案 0 :(得分:3)
$map = array(
"jquery11" => "6PM6t5RFR2w",
"jquery12" => "fE0GC7KFIno",
"jquery13" => "xuFa2R4c6Gc",
"jquery14" => "M971xYWiS7M",
"jquery15" => "oZ_J422z4WI",
"jquery16" => "tYJMitUfSvs",
"jquery17" => "wZPhQzqGzls",
"jquery18" => "Cdwn2JoCYQ0",
"jquery19" => "cKCiNf23Atg"
);
$id = $_GET['id'];
if( array_key_exists( $id, $map ) ) {
echo <<<HTML
<div id="video" >
<iframe width="640" height="360" src="http://www.youtube.com/embed/{$map[$id]}?autoplay=1&controls=2" frameborder="5" allowfullscreen>
</iframe>
</div>
HTML;
}
答案 1 :(得分:1)
创建一个可能的id数组,然后在if语句下使用in_array检查传递的变量是否在数组中,然后从那里分配正确的url。
答案 2 :(得分:1)
未经测试 - 应该工作:
<?php
$request = $_GET['request'];
$accepted_requests = array(1 =>'jquery1', 2 => 'jqery2', 3 => 'jquery3');
$utube_urls = array(1 => 'url', 2 => 'url', 3 => 'url');
if(in_array($request, $accepted_requests)){
foreach($accepted_requests as $k=>$v){
if($request === $v){
foreach($utube_urls as $keys => $vals){
if($k == $keys){
echo $vals;
}
}
}
}
}
&GT;
更新代码:错过$ val上的's' - 现在修复