View first 10 files in a folder

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

View first 10 files in a folder

Post by mickyjune26 »

Hey all. This looks like a pretty cool forum for a newbie like me.

How would I modify the code below to only show the first 10 files?
How would I modify to only show the 11+ records?

Thanks!

Code: Select all

<?php
 if ($handle = opendir('./content/Workspace Lessons/Workspace Lessons/Language Arts')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
	  {
          	$thelist .= '<a class="body_css" href="/content/Workspace Lessons/Workspace Lessons/Language Arts/'.$file.'">'.$file.'</a><br/>';
          }
       }
  closedir($handle);
  }
?>
<P>List of files:</p>
<P><?=$thelist?></p>
<?php
unset($thelist);
?>
Last edited by mickyjune26 on Mon May 17, 2010 11:08 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: View first 10 files in a folder

Post by pickle »

You could implement a counter starting at 1, break() out of the while loop if counter > 10. For 11+, again check the counter & don't show if the counter is < 11. Perhaps not the most efficient method, but it'll work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

Re: View first 10 files in a folder

Post by mickyjune26 »

thank you for the quick response. What would that code look like?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: View first 10 files in a folder

Post by pickle »

That's a great thing for you to figure out for yourself.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

Re: View first 10 files in a folder

Post by mickyjune26 »

Thanks again for the fast response. This is a great site. Do you have any thoughts on where I would find how to create a counter, a while loop and a break?

Thanks!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: View first 10 files in a folder

Post by pickle »

Google. The PHP docs would show you how to create a while() loop and use break.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

Re: View first 10 files in a folder

Post by mickyjune26 »

Thanks. Should be able to figure it out. I will put the while loop around

Code: Select all

$thelist .= '<a class="body_css" href="/content/Workspace Lessons/Workspace Lessons/Language Arts/'.$file.'">'.$file.'</a><br/>';
This is fun learning new things!
mickyjune26
Forum Commoner
Posts: 30
Joined: Mon May 17, 2010 9:52 am

Re: View first 10 files in a folder

Post by mickyjune26 »

Here's the code for the first 5 (2 of which are . and .. so it really only shows 3 or less)

Code: Select all

          <?php
 $i = 0;
 if ($handle = opendir('./content/Games')) { 
   while (false !== ($file = readdir($handle))) 
      { 
          if (++$i<=5 && $file != "." && $file != "..") 
          { 
                $thelist .= '<a class="body_css" href="/content/Games'.$file.'">'.$file.'</a><br/>'; 
          } 
       } 
  closedir($handle); 
  } 
?>

<ul><?=$thelist?></ul>
<?php
unset($thelist);
?>
and for greater than 5 (two of which are . and .. so it really shows greater than 3)

Code: Select all

 <?php
 $i = 0;
 if ($handle = opendir('./content/Games')) { 
   while (false !== ($file = readdir($handle))) 
      { 
          if (++$i>5 && $file != "." && $file != "..") 
          { 
                $thelist .= '<a class="body_css" href="/content/Games'.$file.'">'.$file.'</a><br/>'; 
          } 
       } 
  closedir($handle); 
  } 
?>

<ul><?=$thelist?></ul>
<?php
unset($thelist);
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: View first 10 files in a folder

Post by AbraCadaver »

The expression is evaluated left to right so the ++$i is always executed even if the other expressions are false. If you put it at the end then $i only increments if the other expressions are true:

Code: Select all

if ($file != "." && $file != ".." && ++$i<=5)
Or you could move the increment inside the if:

Code: Select all

          if ($i<=4 &&  $file != "."  && $file != "..")
          {
                $thelist .= '<a class="body_css" href="/content/Games'.$file.'">'.$file.'</a><br/>';
                $i++;
          }
I would however probably use glob().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply