Jump to content

debug output: array sorting funny.


jwortman

Recommended Posts

Hi!

 

My output

 

http://www.spiritaliveon65.org/pages/multimedia.php

 

is not in order!  I wanted to first fix it to sort from jan 1 through dec 31.  But, actually, I'd rather put it in reverse order, I was just trying to do one problem at a time.  If you're bored and want to look at it, I'll use it if it works.

 

Here's my code currently.

 

<?

/*

When this function is called, all mp3 files in the multimedia/servicearchives are listed and linked to as downloadable.

 

Params: None

**/

 

function CreateM3UFile($mp3Name, $m3uName, $dirToCreate){

/*

When this function is called, m3u files are created for the mp3.

Params: $mp3Name as the file name of the mp3 and $mpuName as the file of the m3u and $dirToCreate as the dir where the m3u file will be created.

**/

 

$myFile = $m3uName;

$fh = fopen($dirToCreate . $myFile, 'w') or die("can't open file");

$stringData = "http://www.spiritaliveon65.org/multimedia/servicearchives/" . $mp3Name;

fwrite($fh, $stringData);

fclose($fh);

 

}

 

function ListServices($dirPath){

/*

When this function is called, all mp3 service files are listed in a specified directory.

Params: $dirPath as the path of the services

**/

 

$dh = opendir($dirPath);

 

while (false !== ($file = readdir($dh))) {

 

//Don't list subdirectories

if (!is_dir("$dirpath/$file")) {

$fileParts = explode(".",$file);

 

if ($fileParts[1] == "mp3"){

 

$dateParts = explode("-",$fileParts[0]);

$dateYear = $dateParts[0];

$dateMonth = $dateParts[1];

$dateDay = $dateParts[2];

$dateTitle = $dateParts[3];

$m3uName = mktime() . "-" . rand(1, 3000) . ".m3u";

 

$itemTitle = date("F j, Y", mktime(0, 0, 0, $dateMonth, $dateDay, $dateYear)) . ": " . $dateTitle . ": ";

echo $itemTitle . "[<a href='http://www.spiritaliveon65.org/multimedia/servicearchives/streaming/" . $m3uName . "'>Stream</a>]<br>";

 

CreateM3UFile($file, $m3uName, $dirPath . "/streaming/");

}

}

}

closedir($dh);

}

?>

Link to comment
https://forums.phpfreaks.com/topic/133700-debug-output-array-sorting-funny/
Share on other sites

Give this a try

<?php
/*
When this function is called, all mp3 files in the multimedia/servicearchives are listed and linked to as downloadable.

Params: None
**/

function CreateM3UFile($mp3Name, $m3uName, $dirToCreate){
/*
When this function is called, m3u files are created for the mp3.
Params: $mp3Name as the file name of the mp3 and $mpuName as the file of the m3u and $dirToCreate as the dir where the m3u file will be created.
**/

   $myFile = $m3uName;
   $fh = fopen($dirToCreate . $myFile, 'w') or die("can't open file");
   $stringData = "http://www.spiritaliveon65.org/multimedia/servicearchives/" . $mp3Name;
   fwrite($fh, $stringData);
   fclose($fh);

}

function sortMP3s($a, $b)
{
    if ($a['date_sort'] == $b['date_sort']) { return 0; }
    return ($a['date_sort'] < $b['date_sort']) ? -1 : 1;
}

function ListServices($dirPath){
/*
When this function is called, all mp3 service files are listed in a specified directory.
Params: $dirPath as the path of the services
**/

   $dh = opendir($dirPath);
   $mp3_list = array();

   //Read all mp3 files and data into an array
   while (false !== ($file = readdir($dh))) {

      //Don't list subdirectories
      if (!is_dir("$dirpath/$file")) {
         $fileParts = explode(".",$file);
         
         if ($fileParts[1] == "mp3")
     {
            $dateParts = explode("-",$fileParts[0]);
            $index = count($mp3_list);
		$mp3_list[$index]['date_sort'] = $dateParts[0].$dateParts[1].$dateParts[2];
		$mp3_list[$index]['year'] = $dateParts[0];
		$mp3_list[$index]['month'] = str_pad($dateParts[1], 2, '0');
		$mp3_list[$index]['day'] = str_pad($dateParts[2], 2, '0');
		$mp3_list[$index]['dateTitle'] = $dateParts[3];
		$mp3_list[$index]['file'] = $file;
         }
      }
   }

   //Sort the array
   usort($mp3_list, 'sortMP3s');

   //Process/display the array
   foreach ($mp3_list as $mp3)
{
   $m3uName = mktime() . "-" . rand(1, 3000) . ".m3u";
   $itemTitle = date("F j, Y", mktime(0, 0, 0, $mp3['month'], $mp3['day'], $mp3['year'])) . ": " . $mp3['dateTitle'] . ": ";
       echo $itemTitle . "[<a href='http://www.spiritaliveon65.org/multimedia/servicearchives/streaming/" . $m3uName . "'>Stream</a>]<br>";
       CreateM3UFile($file, $m3uName, $dirPath . "/streaming/");
}
   closedir($dh);
}
?> 

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.