Im learning JQuery and php.
Is it possible to store multiple video links within variables and get php to echo one at random??
My goal is to control my own video ads on my site and I thought this would be a good idea but I dont have a clue where I should look online.
Here is what I was thinking
<?php
$advert1 = 'MyVIDEO1.mp4';
$advert2 = 'MyVideo2.mp4';
$advert3 = 'MyVideo3.mp4';
I want a code that would go here and say: randomly select one of these vars.
echo "At random one of the vars";
?>
I hope Im making sense. help?
You could make an array, like this:
$advert[] = array();
$advert[1] = 'MyVIDEO1.mp4';
$advert[2] = 'MyVIDEO2.mp4';
$advert[3] = 'MyVIDEO3.mp4';
$chosen_one = rand(1,count($advert));
echo $advert[$chosen_one];
You can also use array_rand() instead of shuffle():
<?php
$videos = array("MyVIDEO1.mp4", "MyVideo2.mp4", "MyVideo3.mp4");
echo $videos[array_rand($videos)];
?>
For embedding the video in the right way, have a look at http://www.w3schools.com/html/html_videos.asp
For a start, PHP itself won't exactly play a video. You could use it to echo out one of the URL's to a video. So, bear in mind there is a lot more to do than just select a video.
To answer your question in the code I'd recommend the following:
Try looking up PHP arrays. You want to keep all the videos in an array.
Next up, you'll want to shuffle that array. Then select the first element.
$videos = array("MyVIDEO1.mp4", "MyVideo2.mp4", "MyVideo3.mp4");
shuffle($videos);
echo $videos[0];
This is a simple solution
Place your adverts in an array
<?php
$adverts = array("advert1" => "MyVIDEO1.mp4",
"advert2" => "MyVIDEO2.mp4",
"advert3" => "MyVIDEO3.mp4");
$count = count($adverts);
$rand_advert = rand(1, $count);
echo $adverts['advert'.$rand_advert];
?>
Or alternatively if you don't want to have keys and just put the values in the array straight away
<?php
$adverts = array("MyVIDEO1.mp4",
"MyVIDEO2.mp4",
"MyVIDEO3.mp4");
shuffle($adverts);
echo $adverts[0];
?>
You have need all value in a array then find index randomly and show it.
<?php
$advert = array(
'MyVIDEO1.mp4',
'MyVideo2.mp4',
'MyVideo3.mp4'
);
$total_video = count($advert);
$total_video--; //array index starting from 0 so decrease 1
$random_index = rand(0, $total_video); //array index 0 to 2
$video_to_play = $advert[$random_index];
echo $video_to_play;
?>
Related
I am pulling a lot of information from 1 cell in a database for example. This is a silly. big and amazing. sentence of awesomeness.
Is there a way that i can split the read in php so it comes out in a new line for every full stop.
<?php
$query = "SELECT * from testview";
$res = mysqli_query($con, $query);
$row s =$res->num_rows;
$i = 1;
while($result = mysqli_fetch_array($res)) {
?>
<?php echo $result['QuestionText'];?>
<?php } ?>
I want to split the echo
Going off your examples, lets say
$result['QuestionText'] = "Hello welcome to my supercool website. do you have any time to play a game. no well okay thanks for coming anyway.";
And you want to split this up onto different line by sentence.
Quick way is to replace the periods with a <br>\n
echo str_replace('.', '.<br/>\n', $result['QuestionText']);
A more desirable way (IMO) would be to split the text into an array by the periods.
$aryQText = explode('.', $result['QuestionText']);
Now $aryQText is an array with three elements, each is a sentence from your text. Then you can just loop through the element like so.
foreach($aryQText as $sentence){
echo "$sentence.<br/>\n";
}
This way is much more flexible.
I have some php code that extracts a web address. The object I have extracted is of the form:
WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults
Now in PHP I have called this object $linkHREF
I want to extract the id element only and put it into an array (I'm bootstrapping this process to get multiple id's)
So the command is:
$detailPagePathArray = explode("id=",$linkHREF); #Array
Now the problem is the output of this includes what comes after the id tag, so the output looks like:
echo $detailPagePathArray[0] = WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&w
echo $detailPagePathArray[1] = bf&page=1&
echo $detailPagePathArray[2] = 16123012&source=searchresults
Now the problem is obvious, where it'd firstly picking up the "id" in the "wid" marker and cutting it there, however the secondary problem is it's also picking up all the material after the actual "id". I'm just interested in picking up "16123012".
Can you please explain how I can modify my explode command to point it to the particular marker I'm interested in?
Thanks.
Use the built-in functions provided for the purpose.
For example:
<?php
$url = 'http://www.example.com?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults';
$qs = parse_url($url);
parse_str($qs['query'], $vars);
$id = $vars['id'];
echo $id; // 16123012
?>
References:
parse_url()
parse_str()
if you are sure that you are getting &id=123456 only once in your object, then below
$linkHREF = "WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults";
$str = current(explode('&',end(explode('&id', $linkHREF,2))));
echo "id" .$str; //output id = 16123012
Excuse me. I am beginner for this code web :)
I have 3 images for my banner...
I dont want appear image banner same as random() statement
I have tried an array multidimensional php
$images[0] = ("1.jpg","2.jpg","3.jpg");
$images[1] = ("2.jpg","3.jpg","1.jpg");
$images[2] = ("3.jpg","1.jpg","2.jpg");
I want to change the position every refresh of my image like $images(array) above
My code like this :
<?php
$a=1;
$i=3;
while($a<=$i){
$images[$a] = ("$appear");}
//$appear is list of image above but I create it with random()
?>
I have 2 question for this problem :
I want to get value where $a=1 and $a=2 images appear as $images[1] = ("2.jpg","3.jpg","1.jpg");and$images[2] = ("3.jpg","1.jpg","2.jpg");
Could I get that value? which code would I use?
I want to use javascript for recall $a=1; if $a had finished (for loop again)....
Could you help me?
I am sorry if My Attitude and My Language is fall apart
I hope you can understand My Idea
Thank you for you greatful ^^
Are you thinking about this.....
for refreshing you have to use session variables
<?php
session_start();
if(!isset($_session['a']))
$_session['a'] = 1;
$a = $_session['a']; // retrieves the value even you refreshes the page
$i=3;
while(1){
$images[$a] = ("$appear");
$a = $a + 1;
if($a>3)
$a=1;
$_session['a'] = $a; //stores the value even you refreshes the page
}
?>
it will reset the $a value to 1 when it reaches 3
If is this you are looking for..?
I think you just have some syntax issues.
Arrays in PHP are in this form:
$images = array('1.jpg', '2.jpg', '3.jpg');
if you want a random image use:
$image = array_rand($images);
Cheers!
Ok I'm going to try and explain this the best I can, I have 25 links in this format:
bla bla
First thing first, I need to add these 25 links into an array, which I am bit unsure of how to do it because its html, secondly I need to shuffle the array to choose 7 of them randomly and then display those 7.
Hope someone can help, this is beyond me, thanks in advance.
Ok, a little update, I have found a way of getting 1 html link to display randomly, could anyone help me with getting 7 out?
<?php
// Create the array
$links = array();
$links[0] = 'bla1';
$links[1] = 'bla2';
$links[2] = 'bla3';
// Count links
$num = count($links);
// Randomize order
$random = rand(0, $num-1);
// Print random link
echo $links[$random];
?>
For your second task :
Check array_rand() to retrieve X random values in your array.
http://www.php.net/manual/en/function.array-rand.php
If you care only about displaying these links randomized to the user then you can do with JavaScript like this http://jsfiddle.net/hVZL2/.
If you want to load these links into PHP array and do something with them after you still will have to use JavaScript. Convert the array that I created to JSON, send it via POST to some script that will parse JSON and you will have array of links.
As I can see you have your links on server.
<?php
// Create the array
$links = array();
$links[0] = 'bla1';
$links[1] = 'bla2';
$links[2] = 'bla3';
$links[3] = 'bla3';
$links[4] = 'bla3';
$links[5] = 'bla3';
$links[6] = 'bla3';
// Shuffle the array
shuffle($links);
// Display your links, note that we will display five links out of seven
for ($i = 0; $i < 5; $i++){
echo $links[$i];
}
EDITED
I'm trying to setup a random link at the bottom of all my pages. I'm using the code below, but want to make it so the current page is not included in the random rotation of links.
Example:
I need code to randomly select and display ONE of these links. The exception being, IF article1.php is currently being viewed, I want it to be excluded from the random selection. That way only links to OTHER articles are seen on any given article.
http://mysite.com/article1.php
http://mysite.com/article2.php
http://mysite.com/article3.php
I would use array_rand with something like:
<?php
$links = array(array('url' => 'http://google.com', 'name'=>'google'),
array('url' => 'http://hotmail.com', 'name' => 'hotmail'),
array('url' => 'http://hawkee.com', 'name' => 'Hawkee'));
$num = array_rand($links);
$item = $links[$num];
printf('%s', $item['url'], $item['name'], $item['name']);
?>
Where links makes it easier to build an array. Nevertheless, I think we miss some details about how you grab your links.
What is the mean of "current page"? because the simplest way to do, is just not add the page to the array.
And the use of array_rand avoids confusion with size of array and so.
Edit: I suppose you use a database, so you may have an sql request like:
SELECT myfieldset FROM `articles` WHERE id = 'theid';
So you know the id of the current article. Now you just have to build an array with some other articles with a query like:
SELECT id FROM `articles` WHERE id NOT IN ('theid') ORDER BY RAND LIMIT 5
And build the candidate array with those results.
Each time you randomly choose a URL to display, pop it off of the array and store it in a temporary variable. Then, on the next rotation make your selection and THEN push the previously used URL back into the array.
$lastUrl = trim(file_get_contents('last_url.txt'));
while($lastUrl == ($randUrl = $urls[rand(0, count($urls) - 1)])){}
file_put_contents('last_url.txt', $randUrl);
// ...
echo $randUrl;
Ensures that on each page load, you will not receive the previous URL. This, however is just an example. You would want to incorporate file locking, exception handling (perhaps) or an entirely different storage medium (DB, etc.)
To ensure the URL is not the same as the current, this should do the trick:
// get current URL
$currentUrl = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
// randomize URLs until you get one that doesn't match the current
while($currentUrl == ($randUrl = $urls[rand(0, count($urls) - 1)])){ }
echo $randUrl;
Google "PHP get current URL", and you'll get considerably more detailed ways to capture the current URL. For example, conditions on whether or not you're use HTTPS, to append an 's' to the protocol component.
try the codes below :
$links = array(
'http://mysite.com/article1.php',
'http://mysite.com/article2.php',
'http://mysite.com/article3.php',
'http://mysite.com/article4.php',
'http://mysite.com/article5.php'
);
$currentPage = basename($_SERVER['SCRIPT_NAME']);
$count = 0;
$currentIndex = NULL;
foreach($links as $link) {
if(strpos($link, "/".$currentPage)>-1) $currentIndex = $count;
$count++;
}
if($currentIndex) {
do{
$random = mt_rand(0, sizeof($links) - 1);
} while($random==$currentIndex);
} else {
$random = mt_rand(0, sizeof($links) - 1);
}
$random_link = $links[$random];