Page 1 of 1
View first 10 files in a folder
Posted: Mon May 17, 2010 9:56 am
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);
?>
Re: View first 10 files in a folder
Posted: Mon May 17, 2010 10:00 am
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.
Re: View first 10 files in a folder
Posted: Mon May 17, 2010 11:10 am
by mickyjune26
thank you for the quick response. What would that code look like?
Re: View first 10 files in a folder
Posted: Mon May 17, 2010 11:19 am
by pickle
That's a great thing for you to figure out for yourself.
Re: View first 10 files in a folder
Posted: Mon May 17, 2010 11:34 am
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!
Re: View first 10 files in a folder
Posted: Mon May 17, 2010 11:38 am
by pickle
Google. The PHP docs would show you how to create a while() loop and use break.
Re: View first 10 files in a folder
Posted: Mon May 17, 2010 11:48 am
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!
Re: View first 10 files in a folder
Posted: Thu May 20, 2010 11:59 am
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);
?>
Re: View first 10 files in a folder
Posted: Thu May 20, 2010 12:41 pm
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().