Embed randomly named MP3 - php

Here's my code:
<embed src="/sound/lowyourchicken.mp3"
width="140" height="40" autostart="true" loop="TRUE">
</embed>
I would like the src for the .mp3 to take in to account that there are many randomly named .mp3 files in the /sound/ directory, and to choose one at random each time the page is opened. Any clues for me?
My server is PHP enabled but I'd like to keep this as simple as possible.

This should do it:
$files = glob("/path/to/directory/*.mp3");
$random = array_rand($files)
Then do this:
<embed src="<?php echo $random ?>"
width="140" height="40" autostart="true" loop="TRUE">
</embed>

array_rand returns what random index it chose, so you'll need to do this:
<embed src="<?php $files[ $random ] ?>"

Try This:
It will Work, I used original code found in answers and did some tweaking by adding array($files) in the $random = array_rand(); variable statement
You will first need to put the PHP code in the body like this
<?php
$files = glob("assets/songs/SayYesToLove/*.mp3");
$random = array_rand(array($files));
?>
next add this just outside that php code in the body
<embed src="<?php echo $files[$random]; ?>" width="140" height="40" autostart="true" loop="TRUE">
</embed>
Please Notice the echo output in the src file. This will ensure it gets outputted to your page. Also don't forget to use the ; at the end of every php variable statement as this can through some errors.

Related

Assign Image SRC in php

After retrieve image path from database I want to pass it to <img> tag to show up,
I read this solution, but I do not want to echo it, I want to assign it as src for image tag
I tried this:
$image = mysqli_fetch_array($user_images);
$img1 = $image['file_path'];
$imageData = base64_encode(file_get_contents($img1));
$src = 'data: '.mime_content_type($img1).';base64,'.$imageData;
How can I assign $src to image, I tried this but no luck :(
<img id="img1" width="150" height="150" src="' . $src . '"/>
Thanks in advance.
Inside the HTML you still need to echo the value of $src. This should work:
<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>
Try with PHP tags like below:-
<img id="img1" width="150" height="150" src="<?php echo $src;?>"/>
Reasonable solution I can think of is
<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>
but the code where you get the image source has to be above the image tag, so you have the $src.
You are using PHP language and in Php, you can't pass variable directly in the Tag you have to set a variable in the PHP environment.
To create Php environment you can right <?php ?> or you can use to echo variable <?= ?> between these PHP tags you can pas your variables to echo and assign or anything else what you want to do.
<img id="img1" width="150" height="150" src="<?= $src ?>"/>

Correct way to put PHP inside HTML equal to PHP variable?

What would be the correct way to put a PHP variable inside HTML that is equal to a PHP variable? Sorry if that's a bit confusing, here's what I mean:
$v = '<center><embed width="420" height="236" src="<?= $pcv ?>" type="application/x-shockwave-flash"></embed></center><hr>';
The simplest way would be to use concatenation.
$v = '<center><embed width="420" height="236" src="'.$pcv.'" type="application/x-shockwave-flash"></embed></center><hr>';
John's answer is my favourite, but an alternative:
<center>
<embed width="420" height="236" src="<?php echo $pcv; ?>" type="application/x-shockwave-flash">
</embed>
</center>
<hr/>

calling a php img string in html

Pretty straight forward simple question, can you open a php code block to call image information in html? I don't think I phrased that right. Here is my code:
<img src="../inventory_images/' . <?php echo $item_number; ?> . '.jpg" width="150" height="150" border="2" />
This code is within the tags
I'm just trying to post a photo using the $item_number variable (which is also the name of the image file i.e. $item_number = T3144 and the image file is name T3144.jpg ). Also if there is a better way to accomplish this suggestions are happily accepted. Sorry to take up bandwidth with such a remedial question but for some reason I can't seem to answer this question in research. Thanks for taking the time everyone.
Your code is wrong, try:
<img src="../inventory_images/<?php echo $item_number;?>.jpg" width="150" height="150" border="2" />
with what you have it looks like the code you had would print
src="../inventory_images/' . whateveritem_numberis . '.jpg"
Yes that is perfectly fine, but make sure that this code is in a file that ends with .php or it will not get parsed by PHP. Also, you need to take out the single quotes and periods:
<img src="../inventory_images/<?php echo $item_number; ?>.jpg" width="150" height="150" border="2" />
Unless the above HTML is in an echo statement, you need to change it to this:
<img src="../inventory_images/<?php echo $item_number; ?>.jpg" width="150" height="150" border="2" />
That will in-turn look like this:
<img src="../inventory_images/T3144.jpg" width="150" height="150" border="2" />
Of course, that is going off of your example where $item_number = 'T3144';.
The single quotes and periods are used for concatenating variables inside of strings.

Play audio file on website

I'm just wondering how, and which audio player I should use to allow my visitors to play audio files on my PHP-based website.
Thanks!
try this
<?php
$path = "PATH OF YOUR AUDIO FILE";
$image = "PATH OF YOUR IMAGEFILE";;?>
<div class="demomain">
<br>
<div style="margin: 0 0 0 30px;" id="mediaspace" name="mediaspace"><embed height="395" width="500" flashvars="javascriptid=JW&autostart=false&width=500&height=395&image=<?php echo $image; ?>&file=<?php echo $path; ?>" allowfullscreen="true" wmode="transparent" allowscriptaccess="always" quality="high" name="JW" id="JW" style="undefined" src="http://player.longtailvideo.com/player4.6.swf" type="application/x-shockwave-flash"> </div>
<?php
echo '<script type="text/javascript">
var s1 = new SWFObject("","JW","500","395","9");
s1.addParam("allowscriptaccess", "always");
s1.addParam("wmode","transparent");
s1.addParam("allowfullscreen","true");
s1.addVariable("javascriptid", "JW");
s1.addVariable("autostart","false");
s1.addVariable("image","' . $image . '"));
s1.addVariable("width","500");
s1.addVariable("height","395");';
echo 's1.addVariable("file","' . $path . '");';
echo 's1.write("mediaspace");';
echo '</script>';
?>
</div>
HTML 5 has an audio player tag,see this:
http://www.w3schools.com/html5/att_audio_src.asp
it is supported on major browsers, but it depends on the format of the audio file
you can see this: http://jplayer.org/ a jquery one,
The open source jPlayer works well and is very customisable - uses HTML/CSS for the interface. Will use HTML5 if it can, and fallback to Flash if not. Requires jQuery.
Website: http://jplayer.org/
You can try Flash Player. I think it will suite your requirement. Of course you should have the flash player installed, but practically almost all internet users have the flash player installed in their machines.
try this
<object height="81" width="100%">
<param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F32222617&show_comments=true&auto_play=true">
</param>
<param name="allowscriptaccess" value="always"></param>
<embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F32222617&show_comments=true&auto_play=true&repeat=1" type="application/x-shockwave-flash" width="100%" loop="yes" autoresume="1" repeat="1">
</embed>
</object>
use this <audio controls><source src="<?php echo $pathtofile ?>" type="audio/mpeg">Error loading</audio> It works for me.

how to insert this javascript in php

how to insert this javascript code :
thejavascript
inside img src in php
<img src="http:'.$iv[$j].'.jpg" height="220" width="200" alt="Image '.ucwords($kw).'" "thejavascript">
Thanks
You can use this jQuery script to add and onClick event in your balise img but you must adding an attribute id into your image. An id must be unique in your html page.
$("img#idImage").click(function({
window.open('welcome.html','welcome');
}));
$kw = $ucwords($kw);
"<img id='idImage' src='http:{$iv[$j]}.jpg' height='220' width='200' alt='Image {$kw}' />";
But the best will be to separate attributes height and width into a CSS stylesheet.
Insert the following HTML outside PHP brackets and it should work according to the way you posted it. I'm making a few assumptions, one being that the link you posted wraps the image code and that the PHP variables turn the image into valid code.
<a href="javascript:void(0);" onclick="window.open('welcome.html','welcome')">
<img src="http:<?=$iv[$j]; ?>.jpg" height="220" width="200" alt="Image <?=ucwords($kw); ?>">
</a>
More simple, replace "thejavascript" (with the "") by :
onclick="window.open(\'welcome.html\',\'welcome\')"
A bit different approach, onclick event is on img tag.
$javascript="window.open('welcome.html','welcome')";
$img='<img src="http:'.$iv[$j].'.jpg" height="220" width="200" onclick="'.$javascript.'">';
echo $img;

Categories