TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   need a little help guys... sort function. (http://www.talkphp.com/absolute-beginners/3995-need-little-help-guys-sort-function.html)

Lou_Gato 02-23-2009 08:23 PM

need a little help guys... sort function.
 
I have this code i use to dispay wmv files in a certain directory, but now i want to sort the results in alphabetic order.. can anyone help me with this?

this is the code i have ..
Code:

if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
 while (($file = readdir($dh)) !== false) {
        if($file != '.' & $file != '..' & $file != '' & filetype($dir)=='dir'){
$pieces = explode(".", $file);
$extension = $pieces[1];
if(array_search($pieces[0], $existing)){
if($found == 0) { $found = 0;
}
}else{
if($extension == 'wmv'){                                                       
//echo "$file " . filetype($dir . $file) . "<br>";
echo "<input type='radio' name='trailer' value='".$pieces[0]."'>
<font color='white'> ".$file."</font><br>";
                                                               
                                                                }
                                }
                }
        }// end while
  closedir($dh);
 }
}

thanks everyone.. your help is truly appreciated.

Wildhoney 02-23-2009 08:41 PM

I would say use glob, unless there is a particular reason you're using opendir. The glob function will automatically parse the directory in alphabetical order, and so I've reversed it in the example below to show you how it works to order.

php Code:
/* Directory with or without trailing slash as rtrim will remove it anyway. */
$szDir = './app/controllers/';

/* Glob acquires all the files for us with the extension specified (WMV). */
$aFiles = glob(rtrim($szDir, '/') . '/*.wmv');

/* Glob already outputs in alphabetical order, so reverse to show it's working. */
$aFiles = array_reverse($aFiles);

foreach ($aFiles as $szFile)
{
    /* Echo the radio buttons using the relevant data. */
    printf
    (
        '<input type="radio" name="trailer" value="%s" /><font color="white">%s</font><br />',
        pathinfo($szFile, PATHINFO_FILENAME), basename($szFile)
    );
}

Lou_Gato 02-23-2009 08:46 PM

sorry..
 
yeah i probably should have posted the whole script..
the reason i was using opendir is because i am doing a db query right before it to make sure it does not show the files that are already stored in the database..

Code:

  <?php
                        $result = mysql_query("select * from tbl_videos");

                        $existing = array(0 => "noefind");
                        while($row = mysql_fetch_assoc($result)){
                                $existing[] = $row["trailer"];
                        }
                        if(!$newdir==0){
                                        $dir = "../video/$newdir";
                        }else{
                                //echo "please go back and select a category first <a href=\"addvideo.php\">Select Category to upload to</a>";
                                $dir = "../video/";
                        }
                        // list current directory
                        if (is_dir($dir)) {
                                if ($dh = opendir($dir)) {
                                       
                                        while (($file = readdir($dh)) !== false) {
                                                if($file != '.' & $file != '..' & $file != '' & filetype($dir)=='dir'){
                                                       
                                                        $pieces = explode(".", $file);
                                                        $extension = $pieces[1];

                                                        if(array_search($pieces[0], $existing)){
                                                                if($found == 0) { $found = 0;
                                                                }
                                                        }else{

                                                                if($extension == 'wmv'){                                                       
                                                                        //echo "$file " . filetype($dir . $file) . "<br>";
                                                                echo "<input type='radio' name='trailer' value='".$pieces[0]."'><font color='white'> ".$file."</font><br>";
                                                               
                                                                }
                                                        }
                                                }
                                        }// end while
                                        closedir($dh);
                                }
                        }
                       
                       
             
            ?>


Wildhoney 02-23-2009 09:38 PM

You could still use glob in your current set-up. See the $aExisting variable in the following code, modified from my earlier version.

Sorry to be so persistent, but I still believe the glob function is the best approach. Please correct me if you think I am wrong, and I will take a look at your own code to modify that accordingly :-) !

php Code:
/* Directory with or without trailing slash as rtrim will remove it anyway. */
$szDir = './app/controllers/';

/* Glob acquires all the files for us with the extension specified (WMV). */
$aFiles = glob(rtrim($szDir, '/') . '/*.wmv');

/* Glob already outputs in alphabetical order, so reverse to show it's working. */
$aFiles = array_reverse($aFiles);

/* Get the existing files and don't display those in the subsequent loop. */
$aExisting = array('TestController', 'ErrorController');

foreach ($aFiles as $szFile)
{
    $szFilename = pathinfo($szFile, PATHINFO_FILENAME);
   
    /* If the file exists in the $aExisting array, skip displaying it. */
    if (in_array($szFilename, $aExisting))
    {
        continue;
    }
   
    /* Echo the radio buttons using the relevant data. */
    printf
    (
        '<input type="radio" name="trailer" value="%s" /><font color="black">%s</font><br />',
        $szFilename, basename($szFile)
    );
}


All times are GMT. The time now is 01:29 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0