2012-12-22 31 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' - 現在固定