echo html and php giving syntax error - php

There are similar questions on StackOverflow but I'm still having trouble fixing my problem. This is what I have
<video class="video-js vjs-default-skin" width="100%" poster="sqqsdqd.jpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'><source src="$media['data']['videos']['standard_resolution']['url']" type="video/mp4" /></video>
I need to echo it using php but whenever I try I'm getting a syntax error. This code:
echo '<div class="pic"><img src=" ' . $media['data']['images']['standard_resolution']['url'] . '"></div>';
is working fine but I can't figure out how to do it for the video one, help is appreciated.
Thank you.
Edit:
Sorry my actual code is like this
<?php
if ($media['data']['type'] == 'image') {
echo '<div class="pic"><img src=" ' . $media['data']['images']['standard_resolution']['url'] . '"></div>';
} else {
echo '<video class="video-js vjs-default-skin" width="100%" poster="httjpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'> <source src=" '.$media['data']['videos']['standard_resolution']['url'].'" type="video/mp4" /></video>';
}
?>

Just try this,
<?php
echo 'yourstuff';
?>
<video class="video-js vjs-default-skin" width="100%" poster="sqqsdqd.jpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'><source src="<?php echo $media['data']['videos']['standard_resolution']['url'] ?>" type="video/mp4" /></video>
<?php
echo 'yourstuff';
?>
update:
<?php
if ($media['data']['type'] == 'image') {
echo '<div class="pic"><img src=" ' . $media['data']['images']['standard_resolution']['url'] . '"></div>';
} else {
?>
<video class="video-js vjs-default-skin" width="100%" poster="sqqsdqd.jpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'><source src="<?php echo $media['data']['videos']['standard_resolution']['url'] ?>" type="video/mp4" /></video>
<?php
}
?>
I hope this will help to acheive

This issue comes because you have bad quoting style on this line
'<video class="video-js vjs-default-skin" width="100%" poster="httjpg" data-setup='
See the apos ( single quotes ), there are several ways around this. The previous answer is one of them. Here is another
<?php
if ($media['data']['type'] == 'image') {
echo '<div class="pic"><img src=" ' . $media['data']['images']['standard_resolution']['url'] . '"></div>';
} else {
echo <<<HTML
<video class="video-js vjs-default-skin" width="100%" poster="httjpg" data-setup='{"controls":true, "autoplay": true, "preload": "auto"}'> <source src="{$media['data']['videos']['standard_resolution']['url']}" type="video/mp4" /></video>
HTML;
}
?>
Note that the closing HTML; must be on it's own line with no space before or after it. It's called a HEREDOC

Related

Is it possible to use a if statement and echo or print iframe?

So what I would like to do is there is a variable if the variable = 4 it shows the embedded map, is it possible ? Like use echo or print ?
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3690.394035580924!2d114.14653071466621!3d22.33874584714393!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3404007b6cca3859%3A0xf78318a768321055!2sFlow%20Bookshop!5e0!3m2!1sen!2shk!4v1620637580481!5m2!1sen!2shk" width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy"></iframe>
Assuming this HTML is within a .php file, then just use an if statement:
<?php if ($x == 4) { ?>
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3690.394035580924!2d114.14653071466621!3d22.33874584714393!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3404007b6cca3859%3A0xf78318a768321055!2sFlow%20Bookshop!5e0!3m2!1sen!2shk!4v1620637580481!5m2!1sen!2shk" width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy"></iframe>
<?php } ?>

Php Array accessing

I am trying to access an item from the array, the issue is with the line:
src=$videoArray[0]
I have tried a few ways but none seem to work.
<?php
$videoArray = array(
"//www.youtube.com/embed/nEBHkEeH42Y",
"//www.youtube.com/embed/1GlticqrECU",
"//www.youtube.com/embed/BMOUsI8JIaI",
);
?>
<iframe width="520" height="280" src=$videoArray[0] frameborder="0" allowfullscreen></iframe>
You need <?php ?> tags for the array to echo out, and you are missing the quotes around the src attribute.
<iframe width="520" height="280" src="<?php echo $videoArray[0]; ?>" frameborder="0" allowfullscreen></iframe>
You forgot your PHP tags and echo statement:
<iframe width="520" height="280" src="<?php echo $videoArray[0]; ?>" frameborder="0" allowfullscreen></iframe>
or shorthand syntax:
<iframe width="520" height="280" src="<?= $videoArray[0]; ?>" frameborder="0" allowfullscreen></iframe>
<iframe width="520" height="280" src="<?php $videoArray[0] ?>" frameborder="0" allowfullscreen></iframe>
Maybe need reusable code...
<?php
$videoArray = array(
"//www.youtube.com/embed/nEBHkEeH42Y",
"//www.youtube.com/embed/1GlticqrECU",
"//www.youtube.com/embed/BMOUsI8JIaI",
);
foreach($videoArray as $videoLink) {
?>
<iframe width="520" height="280" src="<?php echo $videoLink; ?>" frameborder="0" allowfullscreen></iframe>
<?php
}
?>

Use a PHP variable in Javascript

How can I use a PHP variable inside the Javascript onclick=function?
<?php
for($i=0;$i<10;$i++){
echo '<li><div><img src='. $mobile_image_link[$i+$p] .' width="160" height="165" alt="" border="0" /></div></div></li>';
}
?>
Just concatenate the strings, and use backslashes to escape the necessary quotes:
echo '
<a href="#"
onclick="showPopUp(\''. $mobile_image_link[$i+$p].'\',
\''. $mobile_image_link[$i+$p].'\');">
<img src="'.$mobile_image_link[$i+$p].'" alt="" />
</a>'
Would result in:
<a href="#"
onclick="showPopUp('http://mylink.com/1',
'http://mylink.com/2');">
<img src="http://mylink.com/image.png" alt="" />
</a>
On a side note, please read the HTML latest specifications, setting tag style using width="", height="" and border="" is deprecated and discouraged
Simply echo php variable in javascript code:
<script type="text/javascript">
var js_variable= <?php echo $php_variable; ?>
</script>
Either use the double quotes approach (php variables are only inserted into double quote strings.
<?php for($i=0;$i<10;$i++){
echo "<li><div><img src=\"$mobile_image_link[$i+$p]\" width="160" height="165" alt="" border="0" /></div></div></li>";}
?>
or do what you already did for the img tag. You'll need to escape some quotes to make it work.
<?php for($i=0;$i<10;$i++){
echo '<li><div><img src="'.$mobile_image_link[$i+$p].'" width="160" height="165" alt="" border="0" /></div></div></li>';}
?>
<?php
for($i=0;$i<10;$i++){
$onclickvariable = "showPopUp('".$mobile_image_link[$i+$p]."','".$mobile_image_link[$i+$p]."');"
echo '<li><div><a href="#" onclick="'.$onclickvariable.'">
<img src='.$mobile_image_link[$i+$p].' width="160" height="165" alt="" border="0" /></a>
</div></li>';}
?>

video file name doesn't load correctly

I'm new to PHP. Everything else works on my site okay but I seem to have an issue with this particular piece of code. The $efn is my file name, and I need to retrieve it from a MySQL database but this code doesn't seem to work. It works in a normal video container but not when using the if statement.
Any ideas how to fix this?
<?php
$video_container = substr($efn, -3);
if ($video_container == 'mp4') {
echo '<video preload="auto" width="640" height="264" poster="../images/<?php echo $thumb ?>" controls="controls" autoPlay="false">
<source src="<?php echo $efn ?>" type="video/mp4">
</video>';
}
else {
echo '<video height="620" width="480" controls>
<source src="<?php echo $efn ?>" type ="video/avi" />
</video>';
}
?>
You have syntax problem when printing the string. Replace the <?php echo $fn ?> section by ' . $efn . '
Like this:
$video_container = substr($efn, -3);
if ($video_container == 'mp4') {
echo '<video preload="auto" width="640" height="264" poster="../images/<?php echo $thumb ?>" controls="controls" autoPlay="false">
<source src="' . urlencode($efn) . '" type="video/mp4">
</video>';
}
else {
echo '<video height="620" width="480" controls>
<source src="' . urlencode($efn) . '" type ="video/avi" />
</video>';
}

PHP/HTML echo a variable in a div

I have a div which works fine
<div id="fb-root"></div>
<fb:like href="<?php echo $link; ?>" send="true" width="450" show_faces="true" font="">
</fb:like>
If I try to use an echo, I don't get the value of $link when I do this.
echo '<div id="fb-root"></div>
<fb:like href="<?php echo $link; ?>" send="true" width="450" show_faces="true" font="">
</fb:like>';
likewise this one too. i cannot get the $link.
echo '<iframe src="http://www.facebook.com/plugins/like.php?app_id=208015255907895&href="',$link,'"&send=true&layout=button_count&width=50&show_faces=true&action=like&colorscheme=light&font=trebuchet+ms&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:21px;" allowTransparency="true"></iframe> '
Any help?
You can't use <?php ... ?> in an echo. Try:
echo '<div id="fb-root"></div><fb:like href="', $link, '" send="true" width="450" show_faces="true" font=""></fb:like>';
echo '<div id="fb-root"></div><fb:like href="'.$link.'" send="true" width="450" show_faces="true" font=""></fb:like>';
you cannot add <?php ?> brackets into your echo
you are messing up ' " and a new php tag
echo '<div id="fb-root"></div>
<fb:like href="'. $link .'" send="true" width="450" show_faces="true" font="">
</fb:like>';
Try This:
echo '<div id="fb-root"></div>
<fb:like href="'.$link.'" send="true" width="450" show_faces="true" font="">
</fb:like>';

Categories