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
mickyjune26
Forum Commoner
Posts: 30 Joined: Mon May 17, 2010 9:52 am
Post
by mickyjune26 » Mon May 17, 2010 9:56 am
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.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Mon May 17, 2010 10:00 am
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
Post
by mickyjune26 » Mon May 17, 2010 11:10 am
thank you for the quick response. What would that code look like?
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Mon May 17, 2010 11:19 am
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
Post
by mickyjune26 » Mon May 17, 2010 11:34 am
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!
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Mon May 17, 2010 11:38 am
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
Post
by mickyjune26 » Mon May 17, 2010 11:48 am
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
Post
by mickyjune26 » Thu May 20, 2010 11:59 am
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);
?>
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Thu May 20, 2010 12:41 pm
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.