homepage Welcome to WebmasterWorld Guest from 54.197.94.241
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Stop List After xx Number of Entries
cookiemonster




msg:4188952
 2:37 am on Aug 19, 2010 (gmt 0)


<?php
function listFolders(){
$path = glob("*",GLOB_ONLYDIR); //the double \\ is not a typo you
//have to escape it
//if the path cannot be found
if(!($path)){
echo("The specified path could not be found.");
return;
}//if

//the path is valid
else{
foreach($path as $i){

echo $i;
echo "<br>";

}//foreach
}//else
}//populateDropdown
?>
<?php echo listFolders() ?>


Hi everyone,

The code above lists every folder in a specified directory. I'd like to know if there's any way to limit the number of displayed folders to, for example, the first 10.

Thanks!

 

dreamcatcher




msg:4189047
 7:06 am on Aug 19, 2010 (gmt 0)

Maybe a for loop instead?

$count = count($path);

for ($i=0; $i<($count>10 ? 10 : $count); $i++) {
echo $path[$i];
}

dc

morehawes




msg:4189132
 10:34 am on Aug 19, 2010 (gmt 0)

Maybe not the most elegant approach but looking at the readdir examples in the PHP docs [uk3.php.net...] you could edit the while loop so it breaks when a certain condition is met :


$count = 0;
while (false !== ($file = readdir($handle))) {
echo "$file\n";
$count++;
if($count == 10) {
break;
}
}


A little crude but you get the idea. You would also need to check that $file is a directory.

rocknbil




msg:4189378
 5:16 pm on Aug 19, 2010 (gmt 0)

Right, I'd be using that . . .but given what you have here,


<?php
function listFolders($path,$recurse_limit=null){
$counter=0;
$dirlist=$dir_errors=null;
//if the path cannot be found
if(! is_dir($path)){
return Array (null,"The specified path could not be found.");
}//if
//the path is valid
else{
foreach($path as $i){
$counter++;
$dirlist .= "<li>$i</li>\n"; // Retentive about semantics is Padawan
if ($recurse_limit and ($counter >= $recurse_limit)) {
break;
}
}//foreach
}//else
return Array($dirlist,$dir_errors);
}//listFolders
?>
//
<?php
// assuming GLOB_ONLYDIR is a constant
$dir_limit = 10;
list($dirs,$errors) = listFolders(GLOB_ONLYDIR,$dir_limit);
echo ($errors)?"Error: $errors":"<ul>$dir</ul>";
?>


You really don't need $dir_errors as you only have one error check, but it may be that you might add more later. So instead of

return Array (null,"The specified path could not be found.");

you could do

$dir_errors .= "<li>The specified path could not be found.</li>";

and

$dir_errors .= "<li>Some other error</li>";

then they both return at the end of the function.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved