Pinch
Pinch

Reputation: 31

Trying to echo a YouTube URL into an iframe in PHP

I'm trying to make a simple page that displays a certain video I add into a variable at the top for each day in a week (monday, tuesdays etc.) However, if I hardcode the YouTube URL into the iFrame, that works. But if I pass in a variable, it doesn't.

iFrame won't show with a variable in the 'src' attribute.

<?php
$monday = "https://www.youtube.com/watch?v=rnPkmozi7Zc";
$tuesday = "https://www.youtube.com/watch?v=rnPkmozi7Zc";
$wednesday = "https://www.youtube.com/watch?v=rnPkmozi7Zc";
$thursday = "https://www.youtube.com/watch?v=rnPkmozi7Zc";
$friday = "https://www.youtube.com/watch?v=rnPkmozi7Zc";
$saturday = "https://www.youtube.com/watch?v=rnPkmozi7Zc";
$sunday  = "https://www.youtube.com/watch?v=rnPkmozi7Zc";

$dayofweek = date("w");

switch ($dayofweek) {
    case 1:
        $videoUrl = $monday;
        break;
    case 2:
        $videoUrl = $tuesday;
        break;
    case 3:
        $videoUrl = $wednesday;
        break;
    case 4:
        $videoUrl = $thursday;
        break;
    case 5:
        $videoUrl = $friday;
        break;
    case 6:
        $videoUrl = $saturday;
        break;
    case 0:
        $videoUrl = $sunday;
        break;
}
?>
<div class="embed-responsive embed-responsive-16by9">
    <iframe class="embed-responsive-item" src="<?php echo $monday; ?>"></iframe>
</div>

Upvotes: 0

Views: 1128

Answers (3)

Saulo M
Saulo M

Reputation: 328

Works for me like this:

<?php
$monday = "rnPkmozi7Zc";
$tuesday = "rnPkmozi7Zc";
$wednesday = "rnPkmozi7Zc";
$thursday = "rnPkmozi7Zc";
$friday = "rnPkmozi7Zc";
$saturday = "rnPkmozi7Zc";
$sunday  = "rnPkmozi7Zc";

$dayofweek = date("w");

switch ($dayofweek) {
    case 1:
        $videoUrl = $monday;
        break;
    case 2:
        $videoUrl = $tuesday;
        break;
    case 3:
        $videoUrl = $wednesday;
        break;
    case 4:
        $videoUrl = $thursday;
        break;
    case 5:
        $videoUrl = $friday;
        break;
    case 6:
        $videoUrl = $saturday;
        break;
    case 0:
        $videoUrl = $sunday;
        break;
}

echo '
    <div class="embed-responsive embed-responsive-16by9">
        <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/'.$videoUrl.'" frameborder="0"></iframe>
    </div>
';

?>

Upvotes: 0

Anis Alibegić
Anis Alibegić

Reputation: 3230

<?php
    $links = array(
        '0' => 'https://www.youtube.com/embed/rnPkmozi7Zc', //Sunday
        '1' => 'https://www.youtube.com/embed/rnPkmozi7Zc', //Monday
        '2' => 'https://www.youtube.com/embed/rnPkmozi7Zc', //Tuesday
        '3' => 'https://www.youtube.com/embed/rnPkmozi7Zc', //Wednesday
        '4' => 'https://www.youtube.com/embed/rnPkmozi7Zc', //Thursday
        '5' => 'https://www.youtube.com/embed/rnPkmozi7Zc', //Friday
        '6' => 'https://www.youtube.com/embed/rnPkmozi7Zc', //Saturday
    );

    $numeric_day = date("w");

    $url_to_load = $links[$numeric_day];
?>

<div class="embed-responsive embed-responsive-16by9">
    <iframe class="embed-responsive-item" src="<?php echo $url_to_load; ?>"></iframe>
</div>

I think this is a little bit more elegant since it avoids switch statements and uses hash tables implemented through PHP associative arrays.

Upvotes: 0

DevQuayle
DevQuayle

Reputation: 358

You must have embed link, like this:

https://www.youtube.com/embed/{video_id}

This link you can find in youtube in section share

Upvotes: 2

Related Questions