TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Array diplaying Folder on top ? (http://www.talkphp.com/general/4114-array-diplaying-folder-top.html)

Peuplarchie 04-08-2009 05:01 PM

Array diplaying Folder on top ?
 
Good day,
here I'm working on an array which read a directory and list the files and folders recursively.

I'm trying to display in bold , and on top of all files, all the folders.


PHP Code:


<?php

$directory 
"Art/";
function 
dirList ($directory
{

    
// create an array to hold directory list
    
$results = array();

    
// create a handler for the directory
    
$handler opendir($directory);

    
// keep going until all files in directory have been read
    
while ($file readdir($handler)) {

        
// if $file isn't this directory or its parent, 
        // add it to the results array
        
if ($file != '.' && $file != '..')
        
        
// If file is directory, mark it in bold.
        
if (is_dir($file)){
        echo 
"<b>";
        echo 
$file;
        echo  
"</b><br/>";
        
        
// Else not styled
        
}else{
        echo 
$file;
        echo  
"<br/>";
        
    }
    }

// Here probably should be my code for sorting the folder on top of the files.


    // tidy up: close the handler
    
closedir($handler);

    
// done!
    
return $results;

}

dirList($directory);





?>

Thanks !

allworknoplay 04-08-2009 05:49 PM

Here, I fixed it for you. I tested this on my /etc/ntp/ directory so just change it to whatever yours is.


Code:

<?php

$directory = "/etc/ntp/";
function dirList ($directory)
{

    $directory_link = "/etc/ntp/";
       
        // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {

        $file = "$directory_link" . $file;
               
                $level1 = "$directory_link" . '.';
                $level2 = "$directory_link" . '..';
               
                // if $file isn't this directory or its parent,
        // add it to the results array
        if ($file != "$level1" && $file != "$level2")
       
        // If file is directory, mark it in bold.
        if (is_dir($file) == TRUE){
        print "<b>$file</b><br/>";
       
        // Else not styled
        }else{
        print "$file<br/>";
       
    }
    }

// Here probably should be my code for sorting the folder on top of the files.


    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}

dirList($directory);





?>


/etc/ntp/ntp2
/etc/ntp/step-tickers
/etc/ntp/keys
/etc/ntp/ntpservers

Peuplarchie 04-08-2009 07:40 PM

Nice, it show the folder in bold, can I make the folder list first and then the files.

allworknoplay 04-08-2009 08:01 PM

Quote:

Originally Posted by Peuplarchie (Post 22901)
Nice, it show the folder in bold, can I make the folder list first and then the files.

I can't tell if you're asking a question or making a statement.

But the current code shows the folders first in bold and the files in regular format...

allworknoplay 04-08-2009 09:09 PM

I do have a question for you.

You create this array to hold the data:

$results = array();


But I don't see anywhere in the function where you actually send the data to the $results array variable. Does PHP implicitly somehow do this behind the scenes?

Thanks..

Peuplarchie 04-08-2009 11:32 PM

I echo ed them before, yes, you are right.
Learning...

Peuplarchie 04-08-2009 11:34 PM

You are right, I have ech-ed them before so the is no point for this line I think now.

Peuplarchie 04-09-2009 12:15 AM

RESOLVED

PHP Code:


<?php

$directory 
"Art/SKYDOME/";
function 
dirList ($directory)
{

    
//create 2 arrays - one for folders and one for files
   
$folders = array();
   
$files = array();

    
// create a handler for the directory
    
$handler opendir($directory);

    
// keep going until all files in directory have been read
    
while ($file readdir($handler)) {

        
// if $file isn't this directory or its parent,
        // add it to the results array
        
if ($file != '.' && $file != '..')
        
        
// If file is directory, mark it in bold.
       
if(is_dir($directory.$file)) { 
        
array_push($folders,$file);
        
        
// Else not styled
        
}else{
        
array_push($files,$file);
        
    }
    }


    
// tidy up: close the handler
    
closedir($handler);

    foreach(
$folders as $folder) {
      echo 
"<strong>".$folder."</strong><br />";
    }

    foreach(
$files as $file) {
      echo 
$file."<br />";
    }
    

}

dirList($directory);

?>


allworknoplay 04-09-2009 12:17 AM

Looks much cleaner....

I know that my code wasn't clean earlier but I knew that you would get the point and clean it up....

Ultimatum 04-09-2009 09:37 AM

I have a few comments on your code.
PHP Code:

while ($file readdir($handler)) { 

because readdir returns a bool, you can check if readdir succeeds or not
PHP Code:

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

And this line isn't really usefull, cause you make the if statement but don't do anything with it.
PHP Code:

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

Change it to something like this
PHP Code:

if(in_array($sFile, array('.''..')) === true) {
continue; 
//skip the file
} else {
// Do something 

And the final thing is this.
PHP Code:

array_push($files,$file); 

According to php.net array_push it is better to use $array[] = $val if you add only one element to array
Quote:

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

sketchMedia 04-09-2009 11:33 AM

we can condense it further with the SPL DirectoryIterator:
PHP Code:

$directory './';
$pDir = new DirectoryIterator($directory);
foreach(
$pDir as $fInf)
{
    if(
$fInf->isDot()) { continue; }
    if(
$fInf->isDir()) 
    { 
        echo 
'<strong>'$fInf->getFilename() , '</strong><br />'
    }
    else
    {
        echo 
$fInf->getFilename() , '<br />';
    }



Kalle 04-09-2009 11:38 AM

Quote:

Originally Posted by sketchMedia (Post 22940)
we can condense it further with the SPL DirectoryIterator:
PHP Code:

$directory './';
$pDir = new DirectoryIterator($directory);
foreach(
$pDir as $fInf)
{
    if(
$fInf->isDot()) { continue; }
    if(
$fInf->isDir()) 
    { 
        echo 
'<strong>'$fInf->getFilename() , '</strong><br />'
    }
    else
    {
        echo 
$fInf->getFilename() , '<br />';
    }



*Hint* The SPL docs at php.net could need a hand =)

Salathe 04-09-2009 01:38 PM

Maybe I'm just misreading this thread but I think Peuplarchie is asking for the folders to be displayed first (in bold) and then the files afterwards.

Using the yummy SPL approach we can just iterate over the directory contents twice:
PHP Code:

$path  './path/to/dir';
$files = new DirectoryIterator($path);

// 1. List only folders
foreach ($files as $file)
{
    if (
$file->isDir() && ! $file->isDot())
        echo 
'<strong>'$file'</strong>'"\n";
}
// 2. List only files
foreach ($files as $file)
{
    if (
$file->isFile())
        echo 
$file"\n";




All times are GMT. The time now is 05:50 AM.

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