Category Archives: Computer Programming

My own RSS for music files from PHP

If you have read the previous post about the Website Music Player than you should be able to understand a little more about what is said in this post.

I was searching for hours and hours the other day and found some snippets of code to generate my music RSS and wow I love IT! I used PHP and xml to generate the RSS format.

I first had thought about how I generated the .xml code from the media player list and then found some things that could help in order to generate the correct information for the RSS feed.

So here is the code that I made that works:
(put this code in a music_rss.php file)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
 
$dir = "mp3";
$url = "http://www.thomasnorberg.com/mediaplayer/playerMultipleMenuPackage";
$email = "tnorberg at thomasnorberg dott com (Thomas Norberg)";
$name = "Tom's Blog";
$n = 0;
$playlist = array();
$fdir = opendir($dir);
while($i = readdir($fdir)) {
   // if a .mp3 string is found, add the file to the array
   if (strpos(strtolower($i),".mp3") !== false){
  	   $playlist[$n] = $i;
           $n++;
      }
}
 
//sort($playlist);
//shuffle($playlist);
//array_multisort($playlist);
 
$ttl = 1440;
$music_rss = "$url/playlist22.php";
header('Content-type: application/xml');
$rss_output = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n";
$rss_output .= "<rss xmlns:atom='http://www.w3.org/2005/Atom/' version='2.0'>\n";
$rss_output .= "<channel>\n\n";
$rss_output .= "<atom:link href='$music_rss' rel='self' type='application/rss+xml' />\n";
$rss_output .= "<title>$name Music RSS</title>\n";
$rss_output .= "<description>Where anything is everything</description>\n";
$rss_output .= "<link>$url/$dir</link>\n";
$rss_output .= "<language>en-us</language>\n";
$rss_output .= "<ttl>$ttl</ttl>\n";
 
for ($j=$n-1; $j>$n-11; $j--) {
   $title = str_replace(".mp3","", $playlist[$j]);
   $title = str_replace("_", " ", $title);
   $other = str_replace(" ", "%20", $playlist[$j]);
   $files = "$dir{$playlist[$j]}";
   $filetime = filectime($files);
   $size = filesize($files);
    $rss_output .= "\n<item>\n\n";
    $rss_output .= "<title>$title</title>\n";
    $rss_output .= "<link>$url/$dir/{$other}</link>\n";  
    $rss_output .= "<pubDate>" . date ("r", $filetime); 
    $rss_output .= "</pubDate>\n";
    $rss_output .= "<enclosure url='$url/$dir/{$other}' type='audio/mpeg' length='$size' />\n";
    $rss_output .= "<guid>$url/$dir/{$other}</guid>\n";  
    $rss_output .= "<author>$email</author>\n";
    $rss_output .= "</item>\n";
}
 
$rss_output .= "</channel>\n</rss>\n";
echo $rss_output;
closedir($fdir);
?>

This is what it should look like. Of course you have to change all the appropriate titles and everything or your won’t have anything to show on your RSS feed, such as the top three lines of code in the music_rss.php file:

1
2
3
4
5
6
<?php
 
$dir = "mp3";
$url = "http://www.thomasnorberg.com/mediaplayer/playerMultipleMenuPackage";
$email = "tnorberg at thomasnorberg dott com (Thomas Norberg)";
$name = "Tom's Blog";

And as always if you have any questions or comments please fell free to ask.

GD Star Rating
loading...
GD Star Rating
loading...

Website Music Player

I am finally able to sit down and tell you about the media player that I put on my website. As you can see to the right is a music player and the code that I used is valid XHTML. So here we go:

I first used the files from PremiumBeat.com and put them on my site. But then I didn’t want to edit the .xml file everytime that I added or deleted a song in the mp3 folder. So I found a script that I had from a long time ago from another mp3 player from another site that I built and had to modify it. So here is all the files and how to add them to your site.

Download .zip file here. (I accidentally deleted it awhile ago the link should work now!)

Upload those files to your server where you want the player to go and remember where you stored them (because you will need that url later to input and grab your files to play.

Then take a look at the sample HTML doc in your editor (NOT MS WORD, preferably something like notepad or editpad lite). Also OPEN the playlist.php file in the same editor. (NOTE THIS IS ALL FROM YOUR SERVER)

You are going to need to change a few things in order to have the music you want instead of mine.

So now that you have the HTML file open and the playlist file open. You need to change the following things:

On the sampleEmbedCode.html:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- End Copy/Paste -->
 
<!--Valid XHTML Code for IE and Firefox to Put in widget sidebar for wordpress-->
<div>
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" width="200" height="250">
    <param name="movie" value="http://www.thomasnorberg.com/mediaplayer/playerMultipleList.swf" />
<param name="FlashVars" value="autoPlay=no&amp;playlistPath=http://www.thomasnorberg.com/mediaplayer/playlist.php&amp;overColor=#822601&amp;playerSkin=2"/>
<!--[if !IE]> <-->
<script type="text/javascript" src="http://www.thomasnorberg.com/mediaplayer/swfobject.js"></script>
<div id="flashPlayer">
  This text will be replaced by the flash music player.
</div>
 
<script type="text/javascript">
   var so = new SWFObject("http://www.thomasnorberg.com/mediaplayer/playerMultipleList.swf", "mymovie", "295", "200", "7", "#FFFFFF");
   so.addVariable("autoPlay","no")
   so.addVariable("playlistPath","http://www.thomasnorberg.com/mediaplayer/playlist.php")
   so.write("flashPlayer");
</script>
<!--> <![endif]-->
</object></div>
 
<!-- End Copy/Paste>

and on the on the playlist.php file:

1
2
$dir = "mp3/";
$url = "http://www.thomasnorberg.com/mediaplayer/playerMultipleMenuPackage/mp3/";

Change everything that has http://www.thomasnorberg.com/mediaplayer/ to where ever you put the MP3 Player folder that you uploaded to your website.

Now when you open your sampleEmbedCode.html file again you should have the IsWar.mp3 playing. Now you can either delete that file from the mp3 folder and add new songs or keep it and add songs either way it is up to you. That is it so let me know if you have any complications that I can help with.

GD Star Rating
loading...
GD Star Rating
loading...

Javascript Confirmation and variations

Ok so this is some really old JavaScript but I used way back when I first started so keep it nice and take a look if you like.

Regular confirmation JavaScript


1
2
3
4
5
6
7
8
9
10
<script type="text/javascript">
<!-- 
function confirmation() { 
var answer = confirm("Welcome to Tom's Blog if you want to come in select \n\"OK\"\nor\n\"Cancel\" if you want to leave") 
if (!answer){ 
alert("Bye Bye Douchey!");
history.go(-1); } 
else{ alert("Thanks for sticking around!") } } 
document.writeln(confirmation()) //This will make the popup come up when page is loaded (So disable if  you want to call it like I have) //--> 
</script>

Code for Confirm button:

1
<form> <input type="button" value="Confirm button" onclick="return confirmation();"> </form>

Check if name is correct, if not bye bye!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script type="text/javascript"> 
<!-- 
function testname(){ 
var name = prompt("Please enter your first name: (Remember it is case sensitive)","Stephen"); 
if (name == "Fred" || name == "Suzi" || name == "Crafty" || name == "Stephanie" || name == "Cindy"){
alert("Welcome to Tom's Blog " +name); 
if (name == "Thomas"){ 
window.location.href="http://www.thomasnorberg.com/wp-login.php"; } 
} 
else { alert("Bye Bye Douchey!") 
window.location.href="http://www.google.com"; 
} 
} 
document.writeln(testname()) //This will make the popup come up when page is loaded (So disable if if you want to call it like I have)
//--> 
</script>

Code for Name prompt button:

1
2
3
<form action="">
<input type="button" value="Name Prompt" onclick="return testname();" />
</form>

GD Star Rating
loading...
GD Star Rating
loading...

wp_get_archives(‘type=alpha’)

Thanks to a great find. I was able to make some sweet adjustments to my site. With some modification to what JTK said on his website I used some really cool drop down boxes to achieve what I wanted on my site. I will give you the code for both.

First I wanted to make all my posts accessible on any post page. Then after I found that I could do that I found out how to make it not so obtrusive. Because if you have a lot of posts and not a lot of space on your page you don’t necessarily want the first option that JTK had.

So I found a different way and possibly a better one. If you look on my single post page you can see the top, right below adsense (two drop boxes), one with posts most recent and the other with posts alphabetized.

Anyway without further ado, THE CODE:

1
2
3
4
5
6
7
8
9
<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> 
  <option value=""><?php echo attribute_escape(__('Pick a new post to go to! (Most recent to oldest)')); ?></option> 
  <?php wp_get_archives('type=postbypost&format=option&show_post_count=1'); ?> </select><br />
<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
  <option value="">
   <?php echo attribute_escape(__('Pick a new post to go to! (Alphabetically A-Z)')); ?>
   </option>
  <?php wp_get_archives('type=alpha&format=option&show_post_count=1'); ?>
</select>
GD Star Rating
loading...
GD Star Rating
loading...

HTML Encoder

Have you ever wanted to encode your html to make it show up inside of um say wordpress post and not be ran through html scripting? Well here yah go: This will output that for you!
(Allthough this will not work in IE) Working on a solution for that (try this)

Source Text :


With Entities Encoded: (Click to Select All)

What it looks like in PRE tags:


Want this code?
If you just copy/paste code below to webpage it should work.
Javascript code with script tags to put in html code/webpage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script type="text/javascript">
//<![CDATA[ 
 
function $(id){ return document.getElementById(id) }
 
function encode_entities(s){
  var result = '';
  for (var i = 0; i < s.length; i++){
    var c = s.charAt(i);
    result += {'<':'&#60;', '>':'&#62;', '&':'&#38;', '\"':'&#34;', '\'':'&#39;', '-':'&#45;', '/':'&#47;', '=':'&#61;', '!':'&#33;', '(':'&#40;', ')':'&#41;', '[':'&#91;', ']':'&#93;', '{':'&#123;', '}':'&#125;', '\n':'&#13;'}[c] || c;
  }
  return result;
}
function do_encode(e){
  $('pre').innerHTML = $('dst').value = encode_entities(e.value)
}
 
//]]> 
 
</script>

Make sure to put htmlencoder_nojstags.js file on your server and don’t just copy paste the below code unless you add: <script type="text/javascript"></script> tags. You also have to add the forms portion of the code too.

note: Javascript code without script tags are to put in *.js file

Forms file/html

1
2
3
4
5
6
7
8
Source Text :
<div id="form">
<textarea cols="80" rows="16" onkeyup="do_encode(this)" onchange="do_encode(this)"></textarea>
With Entities Encoded: (Click to Select All)
<textarea id="dst" cols="80" rows="16" onfocus="this.select()" onclick="this.select()"></textarea>
What it looks like in PRE tags: 
<pre id="pre"></pre> 
</div>

Courtesy of Dan Kogai’s HTML Encoder with a little bit of modification.

GD Star Rating
loading...
GD Star Rating
loading...