Solved

Help With Sorting a Foreach loop into Outputting in a Particular Order

Posted on 2009-05-15
5
244 Views
Last Modified: 2012-05-07
I have a file uploader in my forum, for people to upload albums they have written. I have modified Uber Uploader (a progress bar script) to my needs for this usage. After upload is complete i have it writing an xspf playlist file for an audio player from the mp3 files it finds. My problem is uber uploader has a random (well, i cant find how it does it) way of cycling through its uploaded filelist and does not follow the upload order in its output end. This means my xspf playlist file is not created with the correct track order for the album.

I have now included getid3 so that I can grab id3 data from the mp3. How can I make the foreach loop output the stringdata for the xspf in order of the track field getid3 finds? 1,2,3,4,5 sequentially.

Code below shows what I have working:
function createxspfplayer($_FILE_DATA, $_CONFIG_DATA, $_POST_DATA)

 

{

  

include('../../getid3/getid3.php');

 

$mp3pattern = '/.mp3/i';

$xspffile = $_CONFIG_DATA['absolute_path'] . $_CONFIG_DATA['upload_id'] . ".xspf";

$xspfurl = $_CONFIG_DATA['path_to_upload'] . $_CONFIG_DATA['upload_id'] . ".xspf";

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

 

$stringData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<playlist version=\"0\" xmlns=\"http://xspf.org/ns/0/\">

   <trackList>";

 

fwrite($fh, $stringData);

 

 

 

foreach($_FILE_DATA as $slot => $value){

		$file_slot = $_FILE_DATA[$slot]->getFileInfo('slot');

		$file_name = $_FILE_DATA[$slot]->getFileInfo('name');

                    if($file_size > 0){

 

                            $file_path = $_CONFIG_DATA['path_to_upload'] . $file_name;

 

                          if (preg_match($mp3pattern, $file_name))

                           {

 

                           $getID3 = new getID3;

                           $mp3path = $_CONFIG_DATA['absolute_path'] . $file_name;

                           $mp3info = $getID3->analyze($mp3path);

                           getid3_lib::CopyTagsToComments($mp3info);

 

// @$mp3Info['comments_html']['track'][0] would give me the track number from the mp3, obviously a new/different type of loop must be created to sort, this is what i am asking for help with

 

                           $stringData = "<track>

                           <location>" . $file_path . "</location>

                           <info>" . $file_path . "</info>

                           <title>" . @$mp3info['comments_html']['track'][0] . " " . @$mp3info['tags']['id3v2']['title'][0] . " " . @$mp3info['playtime_string'] . "</title>

                           <link>http://www.aiwazz.com/mdp/</link>

                           </track>";

 

                           fwrite($fh, $stringData);

                           }

 

		}

		else{ echo '' . $file_name . ' <span class="ubrError">Failed To Upload</span>'; }

 

  }

$stringData = "</trackList>

</playlist>";

 

fwrite($fh, $stringData);

 

fclose($fh);

Open in new window

0
Comment
Question by:aiwazz
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 24397047
my thought is to first loop through and build an array by_track[], use ksort then do a foreach on this

$by_track[ mp3Info['comments_html']['track'][0] ] = $slot;

do you need more code?
0
 

Author Comment

by:aiwazz
ID: 24397368
Thank you for taking a look! Admittedly i am no php wiz, just getting going... A little more code would be of great benefit if you have the time to share it. I can understand the gist of were you went but might stumble implementing (havent used php's sort function before). I am at work and will be home in 2 hours to test my code.

I really appreciate your time, thanks again!
0
 
LVL 19

Accepted Solution

by:
Michael701 earned 500 total points
ID: 24397853
untested code, but try this.
<?php

function createxspfplayer($_FILE_DATA, $_CONFIG_DATA, $_POST_DATA)

 

{

  

include('../../getid3/getid3.php');

 

$mp3pattern = '/.mp3/i';

$xspffile = $_CONFIG_DATA['absolute_path'] . $_CONFIG_DATA['upload_id'] . ".xspf";

$xspfurl = $_CONFIG_DATA['path_to_upload'] . $_CONFIG_DATA['upload_id'] . ".xspf";

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

 

$stringData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<playlist version=\"0\" xmlns=\"http://xspf.org/ns/0/\">

   <trackList>";

 

fwrite($fh, $stringData);
 

foreach($_FILE_DATA as $slot => $value)

{

  $file_slot = $_FILE_DATA[$slot]->getFileInfo('slot');

  $file_name = $_FILE_DATA[$slot]->getFileInfo('name');

  if(filesize($_CONFIG_DATA['absolute_path'] . $file_name) > 0) // $file_size was not set

  {

    $file_path = $_CONFIG_DATA['path_to_upload'] . $file_name;
 

    if (preg_match($mp3pattern, $file_name))

    {

      $getID3 = new getID3;

      $mp3path = $_CONFIG_DATA['absolute_path'] . $file_name;

      $mp3info = $getID3->analyze($mp3path);

      getid3_lib::CopyTagsToComments($mp3info);

       

      $by_track[ mp3Info['comments_html']['track'][0] ] = $slot;

    }

  }

}
 

ksort($by_track);

reset($by_track); // resets foreach pointer to the begining of the array

 

foreach($by_track as $track => $slot){

                $file_slot = $_FILE_DATA[$slot]->getFileInfo('slot');

                $file_name = $_FILE_DATA[$slot]->getFileInfo('name');

                    if($file_size > 0){

 

                            $file_path = $_CONFIG_DATA['path_to_upload'] . $file_name;

 

                          if (preg_match($mp3pattern, $file_name))

                           {

 

                           $getID3 = new getID3;

                           $mp3path = $_CONFIG_DATA['absolute_path'] . $file_name;

                           $mp3info = $getID3->analyze($mp3path);

                           getid3_lib::CopyTagsToComments($mp3info);

 

// @$mp3Info['comments_html']['track'][0] would give me the track number from the mp3, obviously a new/different type of loop must be created to sort, this is what i am asking for help with

 

                           $stringData = "<track>

                           <location>" . $file_path . "</location>

                           <info>" . $file_path . "</info>

                           <title>" . @$mp3info['comments_html']['track'][0] . " " . @$mp3info['tags']['id3v2']['title'][0] . " " . @$mp3info['playtime_string'] . "</title>

                           <link>http://www.aiwazz.com/mdp/</link>

                           </track>";

 

                           fwrite($fh, $stringData);

                           }

 

                }

                else{ echo '' . $file_name . ' <span class="ubrError">Failed To Upload</span>'; }

 

  }

$stringData = "</trackList>

</playlist>";

 

fwrite($fh, $stringData);

 

fclose($fh);

?>

Open in new window

0
 

Author Comment

by:aiwazz
ID: 24398612
Just a little bit of fix, thanks for pointing out my missed bit on the filesize...

You rock. I thank you.

here's the final working code:
function createxspfplayer($_FILE_DATA, $_CONFIG_DATA, $_POST_DATA)

 

{

  

include('/path/to/getid3/getid3.php');

 

$mp3pattern = '/.mp3/i';

$jpgpattern = '/.jpg/i';

$jpegpattern = '/.jpeg/i';

$space = ' ';

$underscore = '/_/i';

$xspffile = $_CONFIG_DATA['absolute_path'] . $_CONFIG_DATA['upload_id'] . ".xspf";

$xspfurl = $_CONFIG_DATA['path_to_upload'] . $_CONFIG_DATA['upload_id'] . ".xspf";

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

 

$stringData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<playlist version=\"0\" xmlns=\"http://xspf.org/ns/0/\">

   <trackList>";

 

fwrite($fh, $stringData);
 

foreach($_FILE_DATA as $slot => $value){

		$fname = $_FILE_DATA[$slot]->getFileInfo('name');

                               if (preg_match($jpgpattern, $fname)) {

                         $imglink = $_CONFIG_DATA['path_to_upload'] . $fname;

                    }

                    elseif (preg_match($jpegpattern, $fname)) {

                         $imglink = $_CONFIG_DATA['path_to_upload'] . $fname;

                    }
 

  }

 

foreach($_FILE_DATA as $slot => $value)

{

  $file_slot = $_FILE_DATA[$slot]->getFileInfo('slot');

  $file_name = $_FILE_DATA[$slot]->getFileInfo('name');

  $file_size = $_FILE_DATA[$slot]->getFileInfo('size');

  if($file_size > 0)

  {

    $file_path = $_CONFIG_DATA['path_to_upload'] . $file_name;

 

    if (preg_match($mp3pattern, $file_name))

    {

      $getID3 = new getID3;

      $mp3path = $_CONFIG_DATA['absolute_path'] . $file_name;

      $mp3info = $getID3->analyze($mp3path);

      getid3_lib::CopyTagsToComments($mp3info);

       

      $by_track[ @$mp3info['comments_html']['track'][0] ] = $slot;

    }

  }

}

 

ksort($by_track);

reset($by_track); // resets foreach pointer to the begining of the array

 

foreach($by_track as $track => $slot){

                $file_slot = $_FILE_DATA[$slot]->getFileInfo('slot');

                $file_name = $_FILE_DATA[$slot]->getFileInfo('name');

                $file_size = $_FILE_DATA[$slot]->getFileInfo('size');

                    if($file_size > 0){

 

                            $file_path = $_CONFIG_DATA['path_to_upload'] . $file_name;

 

                          if (preg_match($mp3pattern, $file_name))

                           {

 

                           $getID3 = new getID3;

                           $mp3path = $_CONFIG_DATA['absolute_path'] . $file_name;

                           $mp3info = $getID3->analyze($mp3path);

                           getid3_lib::CopyTagsToComments($mp3info);

 

                           $stringData = "<track>

                           <location>" . $file_path . "</location>

                           <info>" . $file_path . "</info>

                           <title>" . @$mp3info['comments_html']['track'][0] . " " . @$mp3info['tags']['id3v2']['title'][0] . " " . @$mp3info['playtime_string'] . "</title>

                           <image>";

                           fwrite($fh, $stringData);
 

                           if (isset($imglink))

                           {

                             $stringData = $imglink;

                             fwrite($fh, $stringData);

                             }
 

                           $stringData = "</image>
 

                           <link></link>

                           </track>";

 

                           fwrite($fh, $stringData);

                           }

 

                }

                else{ echo '' . $file_name . ' <span class="ubrError">Failed To Upload</span>'; }

 

  }

$stringData = "</trackList>

</playlist>";

 

fwrite($fh, $stringData);

 

fclose($fh);

Open in new window

0
 

Author Closing Comment

by:aiwazz
ID: 31581970
perfect, thank you so much!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
PHP Healthcheck 2 85
Passing variables to stored procedure 3 35
mcrypt_create_iv() is deprecated 4 74
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now