DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PHP Last.fm Chart Creator

09.27.2006
| 6176 views |
  • submit to reddit
        This code displays the Top 50 artists XML feed from last.fm in a nice presentable way on your website. Can easily be modified to work with any other feed from last.fm, find the full guide at http://www.strydominc.za.net/index.php?p=projectdetail&d=phplastfmchart

<?php
/**********************************************************************************************
www.strydominc.za.net
Created by Jurgen Strydom, 19-08-2006, jurgen.strydom@gmail.com
Read the readme.txt
Version 1.01, 05-09-2006
**********************************************************************************************/
?>
<link href="lastfmbar.css" rel="stylesheet" type="text/css">
<div>
<?php
//User settings -> needs your attention
$user = "Alkine"; //Your username
$width = 700; //width of the list

//Code you should not worry about
$file = "http://ws.audioscrobbler.com/1.0/user/$user/topartists.xml";
$xml = simplexml_load_file("$file");
$big = $xml->artist[0]->playcount;
$total = count($xml->artist);
$factor =  $width /$big;
?>
<table width="<?php echo $width ?>" border="0" cellpadding="0" cellspacing="0">  
 <?php
 for ($k=0 ; $k<=$total - 1; $k++) {
 	$barlen = round(($xml->artist[$k]->playcount * $factor), 0);
 ?>
  <tr>
    <td width="<?php echo $width ?>" height="10" valign="center"><table width="100%" border="0" cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      <tr>
        <td width="<?php echo $barlen ?>" height="10" valign="center" class="lastfmbar"><?php 
		if ($barlen >= (($width + 100) /2)) {
			echo "<div align=\"right\">", $xml->artist[$k]->playcount, " - <b>", $xml->artist[$k]->name ,"</b></div>";
		}
		if (($barlen < (($width + 100) /2)) && ($barlen >= ($width / 3))) {
			echo "<div align=\"right\">", $xml->artist[$k]->playcount ," -</div>";
		}
		if ($barlen < ($width / 3)) {
			echo " ";
		}		
		?></td>
        <td width="<?php echo $width - $barlen ?>" valign="center"><?php 
		if ($barlen >= (($width + 100) /2)) {
			echo " ";
		}
		if (($barlen < (($width + 100) /2)) && ($barlen >= ($width / 3))) {
			echo "<div align=\"left\"><b> ", $xml->artist[$k]->name ,"</b></div>";
		}
		if ($barlen < ($width / 3)) {
			echo "<div align=\"left\">", $xml->artist[$k]->playcount, " - <b>", $xml->artist[$k]->name ,"</b></div>";
		}		
		?></td>
      </tr>
    </table></td>
  </tr>
  <?php
  }
  ?>
</table>
</div>
<?php
/**********************************************************************************************
Changelog:

Version 1.01
Fixed a bug that caused the bar sizes to display incorrectly.

Version 1
First release.
**********************************************************************************************/
?>
    

Comments

Snippets Manager replied on Wed, 2006/09/27 - 11:33am

The latest version of this code is available at http://www.flatspike.com/projects/php/php-lastfm-chart-creator/