TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-23-2009, 08:23 PM   #1 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 14
Thanks: 0
Lou_Gato is on a distinguished road
Default 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.
Lou_Gato is offline  
Reply With Quote
Old 02-23-2009, 08:41 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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)
    );
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-23-2009, 08:46 PM   #3 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 14
Thanks: 0
Lou_Gato is on a distinguished road
Default 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);
				}
			}
			
			
              
            ?>
Lou_Gato is offline  
Reply With Quote
Old 02-23-2009, 09:38 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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)
    );
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 11:01 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
The hello world contest Village Idiot The Lounge 9 01-07-2008 02:15 PM


All times are GMT. The time now is 10:43 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design