PHP Random Array with Previous/Next - php

Hopefully this'll be pretty simple for someone to help with, but my PHP is very limited so here goes:
I have a random array set up to pull in a different video on each page refresh, and I've set up a 'Next' link which, if pressed, refreshes the array and outputs the 'next' video. Thing is, it's not the 'next' video in the array as it's being passed through the random function and just outputting whichever is finds. In most cases, since there's only four videos in the array, it's the same video.
Each time the page is visited it needs to be a random video, but if 'Next' is pressed, it needs to continue in the array in a loop.
Here's my code so far:
<div class="video-container">
<?php
$randomNumber = rand(0,4);
$videoArray = array(
'<iframe src="http://player.vimeo.com/video/46808655?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
'<iframe src="http://player.vimeo.com/video/46803192?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
'<iframe src="http://player.vimeo.com/video/46811051?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
'<iframe src="http://player.vimeo.com/video/46817110?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
'<iframe src="http://player.vimeo.com/video/46822673?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
);
echo $videoArray[$randomNumber];
$current_index = array_search($randomNumber, $videoArray);
$next = $current_index - +1;
?>
</div>
<?php if ($videoArray > 0): ?>
Next
<?php endif; ?>
Any help would be mostly appreciated :)

How about something like this:
<div class="video-container">
<?php
if (isset($_REQUEST["video"])) {
$randomNumber = $_REQUEST["video"];
} else {
$randomNumber = rand(0,4);
}
$videoArray = array(
'<iframe src="http://player.vimeo.com/video/46808655?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
'<iframe src="http://player.vimeo.com/video/46803192?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
'<iframe src="http://player.vimeo.com/video/46811051?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
'<iframe src="http://player.vimeo.com/video/46817110?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
'<iframe src="http://player.vimeo.com/video/46822673?title=0&portrait=0&byline=0&color=ffffff" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>',
);
echo $videoArray[$randomNumber];
$current_index = array_search($randomNumber, $videoArray);
$next = $current_index - +1;
?>
</div>
<?php if ($videoArray > 0): ?>
Next
<?php endif; ?>
If the script is called with a parameter called video, it'll choose that as the 'random' number.
There are a couple of other tweaks that could to with adding, such as making sure that the chosen video exists, as well as thinking about what to do if someone hits Next when they're on the final video.

First, you should store only urls in your array.
Second, you should pass the id for the next video as url parameter in the Next link.
<div class="video-container">
<?php
// Array with video urls
$videoArray = array(
'http://player.vimeo.com/video/46808655?title=0&portrait=0&byline=0&color=ffffff',
'http://player.vimeo.com/video/46803192?title=0&portrait=0&byline=0&color=ffffff',
'http://player.vimeo.com/video/46811051?title=0&portrait=0&byline=0&color=ffffff',
'http://player.vimeo.com/video/46817110?title=0&portrait=0&byline=0&color=ffffff',
'http://player.vimeo.com/video/46822673?title=0&portrait=0&byline=0&color=ffffff'
);
// Verify if "videoid" has been passed and it is valid
if (isset($_GET["videoid"])
&& is_numeric($_GET["videoid"])
&& ($_GET["videoid"] >= 0)
&& ($_GET["videoid"] < count($videoArray))
{
// videoid is valid, use it
$videoid = $_GET["videoid"];
}
else
{
// videoid is invalid or not set, generate random videoid
$videoid = rand(0, count($videoArray) - 1);
}
?>
<iframe src="<?php print($videoArray[$videoid]); ?>" width="650" height="366" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>
<?php
// calculate id for next video with overflow
$nextid = $videoid + 1;
if ($nextid >= count($videoArray))
$nextid = 0;
?>
</div>
Next

You have to set a $_GET parameter
Next
and randomNumber:
<?php
$videoArray = array(/* */);
if(isset($_GET['v']) && (int)$_GET['v'] < count($videoArray)){
$randomNumber = (int)$_GET['v'];
} else {
$randomNumber = rand(0,4);
}
$current_index = $videoArray[$randomNumber];
$next = $current_index+1;
if($next >= count($videoArray)){ $next = 0; }
........

Before any output of your script, type:
session_start();
Then do
if(!isset($_SESSION['curVid'])) {
$_SESSION['curVid'] = 0;
$_SESSION['videos'] = $videoArray;
shuffle($_SESSION['videos']);
}
if(isset($_GET['next'])) {
$current = intval($_GET['next']);
if($current > count($_SESSION['videos'])) {
$current = 0;
}
$_SESSION['curVid'] = $current;
}
$current = $_SESSION['curVid'];
$video = $_SESSION['videos'][$current];
$next = $_SESSION['curVid']+1;
echo "Next video";

Related

Get SRC from embed code and put it in own iframe, because it needs to go that way

I have a submit form for adding videos on my wordpress site that I create this code...
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url = preg_replace($search,$replace,$url1);
?>
<iframe width="560" height="315" src="<?php echo $url; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
This code will take youtube url and change it to embed code and add src to iframe, also i can post src source from embedable source and it will also work, but...
What I want to do now is if in field I put
<iframe width="560" height="315" src="https://www.youtube.com/embed/videoid" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
take https://www.youtube.com/embed/videoidfrom code above and put it in $url.
When I try it with existing code, my output is...
<iframe width="560" height="315" src="<iframe width=" iframe-embed"="" scrolling="no" frameborder="0" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
Update:
I found the way how embed codes will be extracted its via this function...
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url2 = preg_replace($search,$replace,$url1);
$url3 = preg_match('~iframe.*src="([^"]*)"~', $url2, $result);
$url4 = $result[1];
?>
<iframe width="560" height="315" src="<?php echo $url4; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
Now, I need to combine first function and seconds one, php code should check if its embed code that has SRC value to use preg match for src andf extract src, else it should use first function.
Something like check if SRC value exists, and than use...
$url3 = preg_match('~iframe.*src="([^"]*)"~', $url2, $result);
$url4 = $result[1];
otherwise...
$url4 = $url2;
<?php
$url1 = get_post_meta( $post->ID , 'video_play' , true );
$search = '#(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x';
$replace = 'http://www.youtube.com/embed/$2';
$url2 = preg_replace($search,$replace,$url1);
$url3 = '<iframe width="560" height="315" src="'.$url2.'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
$url4 = preg_match('~iframe.*src="([^"]*)"~', $url3, $result);
$url4 = $result[1];
?>
<iframe width="560" height="315" src="<?php echo $url4; ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

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.

php preg_replace Replace specific string with specific valus

I studied following example
$copy_date = "Copyright 1999";
$copy_date = preg_replace("([0-9]+)", "2000", $copy_date);
here any numeric will be replaced by 2000
But in following example I am confused !!
How to replace
width="anything" with value 280
Want to Substitute anything that appears after
width="
with
width="280"
example
width="481"
will be width="280"
Another example.....
<iframe width="680" height="480" src="//www.youtube.com/embed/RTcgXcz-_G0" frameborder="0" allowfullscreen></iframe>
after preg_replace
should become
<iframe width="280" height="200" src="//www.youtube.com/embed/RTcgXcz-_G0" frameborder="0" allowfullscreen></iframe>
use this:
$copy_date = '<iframe width="680" height="480" src="//www.youtube.com/embed/RTcgXcz-_G0" frameborder="0" allowfullscreen></iframe>' ;
$pattern = '/width="\d+"/i' ;
$new_style = 'width="280"' ;
$new_copy_date = preg_replace($pattern, $new_style, $copy_date) ;
echo $new_copy_date;

Stripping out unwanted embed code

I am currently looking at a piece of code for a friend and I am trying to strip out a piece of unwanted code but not sure how I can achieve what I want.
CODE:
<?php
$blah = '<iframe src="//player.vimeo.com/video/82444237" width="500" height="281"
frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<p>KING OF THE BEASTS from
John Wiley on
Vimeo.</p>';
echo($blah);
?>
In this code the website is showing: from on
I want the start point of the input to be iframe and the endpoint to be /iframe.
Without manually making sure the iframe only is selected, any suggestions on how to achieve this?
Try this:
function blah($postTag)
{
//TERMINATES THE STRING AT </IFRAME>
$exploder = explode("iframe",$postTag);
//CALLS THE STRING UNTIL THE FIRST IFRAME INPUT AND CLOSES THE IFRAME TAG
$cleaned = "<iframe".$exploder[1]."iframe>";
return $cleaned;
}
$blahblah = blah('<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p>KING OF THE BEASTS from John Wiley on Vimeo.</p>');
That should do it.
A simple strstr() will do the job.
<?php
$blah = '<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p>KING OF THE BEASTS from John Wiley on Vimeo.</p>';
echo strstr($blah,'</iframe>',true)."</iframe>";
OUTPUT
<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
$blah = '<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p>KING OF THE BEASTS from John Wiley on Vimeo.</p>';
echo substr($blah, 0, strpos($blah, '</iframe>')) ."</iframe>";
Output:
<iframe src="//player.vimeo.com/video/82444237" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>

Get youtube/vimeo id with pregmatch

How can I get the video ID for youtube and vimeo videos embed code? A youtube video embed code looks like this:
<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>
where the ID in this is dbPmWbqAJPs.
A vimeo embed code looks like this:
<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
where the ID in this example is 62468031.
I don't want to replace those exact ids, but rather have a pregmatch/pregreplace that finds the id in any embed of vimeo and of youtube.
try this
vimeo
$string='<iframe src="http://player.vimeo.com/video/62468031?color=00ff00" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
preg_match_all( '~http://player.vimeo.com/video/(.*?)\?~si',$string,$M);
echo ($M[1][0]);
youtube :
$string='<iframe width="560" height="315" src="http://www.youtube.com/embed/dbPmWbqAJPs" frameborder="0" allowfullscreen></iframe>';
preg_match_all( '~http://www.youtube.com/embed/(.*?)\"~si',$string,$M);
echo ($M[1][0]);
Try below code for php, it works without pregmatch
i.e. :
http://vimeo.com/22222222
http://player.vimeo.com/video/22222222
http://vimeo.com/channels/staffpicks/22222222
http://vimeo.com/groups/groupsname/22222222
PHP Code :
$vimeoUrl = 'http://vimeo.com/channels/channelsName/22222222';
$fetchVimeoIdArr = explode('/', $vimeoUrl);
$idCounter = count($fetchVimeoIdArr) - 1;
$vimeoId = $fetchVimeoIdArr[$idCounter];

Categories