2012-12-22 30 views
1

减少请求我犯了一个video.php页面将显示来自YouTube根据YouTube上的视频ID的视频,所以我做了类似的东西任何想法通过GET

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> 

    <? 
    } 

?> 

任何想法来改变这种方法会减少获取请求吗?我试图制作关联数组,但我认为它是一样的

回答

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; 
} 
+0

,谢谢esailija这是一个很好的解决方案 –

1

创建一个可能的id的数组,然后检查在if语句下使用in_array传递的变量是否在数组中,然后从那里分配正确的网址。

+0

感谢mrtchie,但检查VAR需要相同的GET请求,因为访问者会选择从另一个页面打开视频,并且我想知道他选择观看的视频是什么,并且这将只能通过获取视频ID来获得ID号码 –

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; 
            } 

          } 

        } 


       } 


    } 

>

更新CODE:错过了$ val中的 'S' - 现在固定