Jump to content

Sort Array By Date for XML Playlist


shansen1111

Recommended Posts

Hello-

I am outputting an XML playlist. Right now the code I am using sorts the playlist alphabetically, I would like it to sort by date uploaded. any help would really be appreciated.

 

I found a snippet of code that works in another XML generator I have, but not in this one:

$items[$entry]['mtime'] = filemtime($entry);

 

Here is the code I am currently running to generate the playlist:

<?
/**
* Produce an XPSF playlist on the fly from files in a given folder
*
* Code by Lewis Smith http://www.lasmit.com
* Code released under GNU General Public Licence - http://www.gnu.org/licenses/gpl.html
*
* If you use this software and find it useful please drop me an email - [email protected]
* I'd like to build a list of people using the script which I'll publish on my site, so please do get in touch.

* Code uses snippets from http://getid3.sourceforge.net/source2/d ... dhtml.phps
* Using getID3 library from http://getid3.sourceforge.net/
*/





// Definitions
define("ROOT",                  "blog.iso50.com");
define("PATH",                  ".");   
define("MP3_PATH",              "audio");                  

require_once('getid3/getid3.php');                  // Include the getId3 library

$xmlOutput = "";

// Create ID3 object
$getid3 = new getID3;
$getid3->encoding = 'ISO 8859-1';

$dir = "../test";
// Read directory
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            $full_name = "$dir/$file";
            if (is_file($full_name)) {
               
                 $files[$full_name] = $file;
           
            }
            elseif ($file[0] != '.') {
                $dirs[$full_name] = $file;
            }
       
           
       }
               
      closedir($dh);
   }
}

// Produce output
if ($files) {



$xmlOutput .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">
       
    <trackList>";

END_HEADER;


        // Windows Time sort
//              $files[$file]['mtime'] = filemtime($file);

//  if ($_REQUEST['shuffle']=="true") {
//      shuffle($files);
//  } else {
    asort($files);
//  }



   
    $counter = $i = 0;
    foreach ($files as $full_name => $short_name) {

        $getid3->Analyze($dir . "/" . $short_name); 
       
        $artist = $title = '';
            if (@$getid3->info['tags']) {
            foreach ($getid3->info['tags'] as $tag => $tag_info) {
                if (@$getid3->info['tags'][$tag]['artist'] || @$getid3->info['tags'][$tag]['title']) {
                    $artist = @$getid3->info['tags'][$tag]['artist'][0];
                    $title  = @$getid3->info['tags'][$tag]['title'][0];
                    $album  = @$getid3->info['tags'][$tag]['album'][0];
                    break;
                }
            }
        }
       
        $url = ROOT . "/" . MP3_PATH . "/" . $short_name;
       
        if (strpos($url, "http://") === false) {
            $url = "http://" . $url;
        }
       
        $info = ROOT;
       
        if (strpos($info, "http://") === false) {
            $info = "http://" . $info;
        }
       

        $xmlOutput .= "<track>
            <location>{$url}</location>
           
            <!-- artist or band name -->
            <creator>{$artist}</creator>
           
            <!-- album name -->
            <album>{$album}</album>
           
            <!-- name of the song -->
            <title>{$title}</title>
        </track>"; 

END_TRACK;
    }
   
    $xmlOutput .= "</trackList>
</playlist>";
END_FOOTER;
}

print $xmlOutput;
$myFile = "playlist.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $xmlOutput;
fwrite($fh, $stringData);
fclose($fh);
?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/137287-sort-array-by-date-for-xml-playlist/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.