Php Array accessing - php

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
}
?>

Related

how to embed link youtube to be dynamic in codeigniter

I want to display the youtube link stored in the database and just call the link but it can't
<div class="divideo">
<iframe width="560" height="315" src="<?php echo $link; ?>" frameborder="0" allowfullscreen></iframe>
</div>
but youtube can't connect
<div class="divideo">
<iframe width="560" height="315" src="<?php echo base_url(); ?>/path/<?php echo $link ?>" frameborder="0" allowfullscreen></iframe>
</div>
You should have to give the proper path of the video link if that link is in your server if you are using an external source then use a proper link in your src tag.

Trying to get multiple randomised result returned from mysql database

Hi I have spent most of the day on this and turn to you in the hope someone can help. I need to generate a random display 5-10 embedded youtube vids on a page without duplication. I can get the videos to display (but all of the same artist) when I refresh it displays a new set of videos but again all of the same artist. I know the answer is probably very simple but I just can't see it.
Any help or pointers would be most appreciated.
The initial php code is:
$result = mysql_query("SELECT Youtube FROM artist ORDER BY RAND() LIMIT 5");
$test = mysql_fetch_array($result);
if (!$result)
{
die("Error: Data not found..");
}
$Youtube=$test['Youtube'];
With the output being:
<div class="ws_images"><ul>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<? php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</ul></div>
This should work. I changed your code and also reduced it by a while loop.
<?php
$mysqli = new mysqli("127.0.0.1", "root", "password", "database name");
$mysqli->set_charset('utf8mb4'); //optional for when you use utf8mb4 charset (recommended)
$getYouTubeLinks = "SELECT DISTINCT Youtube FROM artist ORDER BY RAND() LIMIT 5";
$getRows = $mysqli->query($getYouTubeLinks);
if($getRows->num_rows > 0) {
while($row = $getRows->fetch_assoc()) {
$iframes .= '<iframe width="300" height="300" src="https://www.youtube.com/embed/'.$row['Youtube'].'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe><br>';
}
}
?>
<div class="ws_images"><ul>
<?=$iframes;?>
</ul></div>
$result = mysql_query("SELECT DISTINCT Youtube FROM artist ORDER BY RAND() LIMIT 5");
Mysql Documentation.

How can I replace a soundcloud url to an iFrame (PHP)

How can I detect and replace a soundcloud url in a text with an iframe using PHP:
For example:
This:
https://soundcloud.com/s/eminem-ft-dr-dre-old-time-sake
Into this:
<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/140068709&color=00aabb&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>
function linkifySoundcloudURLs( $text )
{
$text = preg_replace('#https{0,1}:\/\/w{0,3}\.*soundcloud\.com\/([^< ]+)#ix',
'<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https://soundcloud.com/$1&color=00aabb&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>',
$text);
return $text;
}
Works for me.
I'm not sure I understand you correctly, but something like this?
<?php
$url = "https://soundcloud.com/aathmigan/eminem-ft-dr-dre-old-time-sake";
?>
<iframe width="100%" height="166" scrolling="no" frameborder="no"
src="<?php echo $url; ?>"></iframe>

Hidding iframe if specific value?

I want to hide iframe if there is no $content present.
<div class="video">
<iframe width="600px" height="400px" src="'.$content.'?autoplay=1" frameborder="0" scrolling="no" allowfullscreen style="visibility:hidden;" onload="this.style.visibility=\'visible\';"></iframe>
</div>
In my case if $content is not present, iframe loads src="?autoplay=1"
I think this will do the trick:
<?php
// Test if variable is set and has content in it
if( isset($content) && $content != "") {
echo '<div class="video"><iframe width="600px" height="400px" src="'.$content.'?autoplay=1" frameborder="0" scrolling="no" allowfullscreen style="visibility:hidden;" onload="this.style.visibility=\'visible\';"></iframe></div>'
}
?>
This way the iframe will not be printed in the html in case of no content.

any idea to decrease requests via GET

i made a video.php page that will show the videos from youtube depending on id of the video on youtube,so i made something like that
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>
<?
}
?>
any idea to change this method that will decrease get requests?i tried making associative array ,but i think it's same
$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;
}
Create an array of your possible ids, and then check if the variable being passed is in the array by using in_array under an if statement, and then just assign the proper url from there.
Untested - should work tho:
<?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;
}
}
}
}
}
?>
UPDATED THE CODE: Missed the 's' on $val - fixed now

Categories