0

Keep in mind I am fairly new to PHP..

So what I am currently trying to do is pull information from a database (AirTable) and display it on a page (which so far I have succeeded in.) I need to embed a video alongside the numerical data, and it would be a different video for each set of data displayed. Not an issue, I have a field in the database that holds the video ID and a variable that calls that ID for each entry. What I am having trouble doing is embedding the YouTube video into the PHP code.

echo "<br>". $e["Test #"]."<br>". $e["RPM"]."<br>". $e["Axial Cut"]."<br>";

So the line of code I am using to embed videos is

<iframe width="250" height="140" src="http://www.youtube.com/embed/IDHERE?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

And this works outside of PHP, but when I try to add it to the echo, it comments out the code after the "http://" so what do I need to fix? It does need to go into the PHP, because I am replacing the part of the URL with the video ID variable so that the video pulls the proper one from the database for each entry.

Alex
  • 7
  • 4
  • If `http://` is commenting your code, that means it is not in a string. Maybe check your quotes / double-quotes (don't forget about escaping characters if needed) – Seblor May 15 '18 at 13:16
  • 1
    You'll want to wrap the whole statement in double quotes and then escape the double quotes inside of the string (i.e. `echo " – War10ck May 15 '18 at 13:16
  • What does a var_dump($e); show you? – Adam May 15 '18 at 13:16
  • _“You'll want to wrap the whole statement in double quotes”_ - [no, you don’t.](http://php.net/manual/en/language.basic-syntax.phpmode.php) – CBroe May 15 '18 at 13:21

2 Answers2

1

Be careful to only use single quotes in your HTML tag when you use double quotes in the PHP code for echoing - or vice versa (otherwise the quotes for the HTML attributes will close the echoed string earlier than you want it to):

echo '<iframe width="250" height="140" src="http://www.youtube.com/embed/IDHERE?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';

EDIT/ADDITION after comment:

Together with your variable, that should be

echo '<iframe width="250" height="140" src="http://www.youtube.com/embed/'.$vidID.'?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';

(i.e. interrupt the echoed string with a single quote, add a dot, the variable name, another dot and continue the string again with another single quote) To answer

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • Thanks, this helped. the $vidID variable returns the string of text from the database that I need to insert into the link where it says "IDHERE", so how would I put the $vidID variable into that link? – Alex May 15 '18 at 14:16
  • please note the addition to my answer as a reaction to your comment – Johannes May 15 '18 at 14:24
0

Try this:

<?php

$videoid = "ZKFwQFBwQFU"; // Your Video ID

echo "<iframe width=\"250\" height=\"140\" src=\"http://www.youtube.com/embed/" . $videoid . "?rel=0\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe>";

If you don't use \" around HTML attribute values, " will be interpreted as the end of string. See https://stackoverflow.com/a/11036433/6523409 for details.

Filip Š
  • 746
  • 2
  • 13
  • 22