1

我制作了一个 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>

    <?
    }

?>

有什么想法可以改变这种方法来减少获取请求吗?我尝试制作关联数组,但我认为它是一样的

4

3 回答 3

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;
}
于 2012-12-22T08:53:29.657 回答
1

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

于 2012-12-22T08:38:03.410 回答
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;
                                    }

                            }

                    }


                }


    }

?>

更新了代码:错过了 $val 上的 's' - 现已修复

于 2012-12-22T08:51:19.710 回答