Jump to content

Help sorting


gavakie

Recommended Posts

I need this php script to pull files alphabetically?

I have this script that pulls files from a drop down to play automatically on my site. how do I get it to play them in order? they are in order in there folders but wont play in order on the player.

 

<?php
require_once('includes/config.inc.php'… 

$page_title = 'Music';



// Start output buffering:

ob_start();



// Initialize a session:

session_start();



// Check for a $page_title value:

if (!isset($page_title)) {

$page_title = 'User Registration';

}



// If no first_name session variable exists, redirect the user:

if (!isset($_SESSION['first_name'])) {



$url = BASE_URL . 'index.php'; // Define the URL.

ob_end_clean(); // Delete the buffer.

header("Location: $url");

exit(); // Quit the script.



}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…

<?php 

$mp3_list = '';



if ( !empty($_POST["musicdir"]) ) { //Get folder info

$dir = $_POST["musicdir"];

$base = $_POST["basepath"];

$wbase = str_replace('\\', '/', $base);

$pattern = "\.(mp3)$";



if ( strlen($dir) > 0 ) {

$wdir = $_SERVER['DOCUMENT_ROOT'] . $base . '//' . $dir; //made a change here



if ( file_exists($wdir) && is_dir($wdir) ) {



if ($handle = opendir($wdir)) {

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



if ($file != '.' && $file != '..') {



if( eregi($pattern, $file) ) {

//echo $wbase . '/' . $dir . '/' . $file . "
";

$mp3_list .= $wbase . '/' . $dir . '/' . $file . ',';

}

}

}



closedir($handle);

}

}

} 

}



function loadMusic($path) {

$out = '';



if (strlen($path) > 0) {

$path = $_SERVER['DOCUMENT_ROOT'] . $path;



if ( file_exists($path) && is_dir($path) ) {

if ($handle = opendir($path)) {

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

if ($file != '.' && $file != '..') {

if ( is_dir($path . '//' . $file) ) { // Only list directories 

$out .= '<option value="' . $file . '">' . $file . '</option>';

}

}

}

closedir($handle);

}

}

}



return $out;

}

?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Music, Baby!</title></title>

<script type="text/javascript" src="/files/audio-player/audio-player.js…

<style type="text/css">

body {

text-align: center;

}

#contents {

margin: 0 auto;

}

</style>

<script type="text/javascript">
//<![CDATA[

AudioPlayer.setup("files/audio-player/pl… { 

width: 290, 

initialvolume: 25, 

transparentpagebg: "yes", 

left: "000000", 

lefticon: "FFFFFF" 

}); 



function pushMusic(files) {

eval('AudioPlayer.embed("player", {soundFile: "' + files + '", autostart: "no"})');

}
//]]>

</script>







</head>

<body>

<form id="music" action="" method="post" >

<div id="contents">

<select name="musicdir" id="musicdir">

<option value="">Select One...</option>

<?php

$music_path = '/files/music';

echo loadMusic($music_path);

?>

</select>

<input type="hidden" id="basepath" name="basepath" value="<?php echo $music_path ?>" />

<input type="submit" id="submit" value="Load Music" />




<div id="player"> </div>

</div>

</form>

<script type="text/javascript">

<?php 

//echo 'alert("' . strlen($mp3_list) . '");';

if ( strlen( $mp3_list ) > 0 ) {

echo 'pushMusic("' . $mp3_list . '");';

}

?>

</script>

</body>

</html>

<?php // Flush the buffered output.

ob_end_flush();

?>
Category
Computers & Internet > Programming & Design

Link to comment
https://forums.phpfreaks.com/topic/174457-help-sorting/
Share on other sites

This should be easy enough to do.

 

Instead of reading the filenames and immediately adding them to your $mp3_list as you do below:

$mp3_list .= $wbase . '/' . $dir . '/' . $file . ',';

 

Add them to an array instead:

$mp3_array[] = $wbase . '/' . $dir . '/' . $file . ',';

 

Then after you close your while loop sort your array like so:

sort($mp3_array);

 

Follow this with another loop to add the contents of $mp3_array to $mp3_list:

for($i=0; $i <=count($mp3_array); $i){
     $mp3_list .= $mp3_array[$i];
}

 

Use whatever code you deem fit for your loops and arrays as the above example is easy to read but not well optimized.

 

Hope this helps,

Handy PHP

Link to comment
https://forums.phpfreaks.com/topic/174457-help-sorting/#findComment-920515
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.