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
mairaj
Forum Newbie
Posts: 11 Joined: Mon Sep 20, 2004 2:40 am
Post
by mairaj » Wed Nov 10, 2004 3:43 pm
Hi,
I have a task I am stuck with and hope I'll get some help here. My problem follows...
I need to list filenames from a directory but only 10 at a time. Say the directory has 25 files then the first 10 file names should get displayed on the webpage and a link saying 'Next' at the bottom of the page, clicking which should show the next 10 file names.
Ex:
file1
file2
file3
file4
file5
file6
file7
file8
file9
file10
Next...
When clicked next...
file11
file12
file13
file14
file15
file16
file17
file18
file19
file20
Next...
Presently I am using the following code to display all the file names at once...
Code: Select all
$path = 'logs/';
$mydir = dir($path);
while(($file = $mydir->read()) !== false) {
if($file !== "." && $file !== "..") {
echo "<a href='readfile.php?filename=$path$file'><font face='verdana' size='2' color='#a00000'>$file</font><br>";
}
}
$mydir->close();
In simple words, I want to limit the display of filenames from a directory to 10. Can anybody please help me???
Cheers.
rehfeld
Forum Regular
Posts: 741 Joined: Mon Oct 18, 2004 8:14 pm
Post
by rehfeld » Wed Nov 10, 2004 4:24 pm
this should help get you going
Code: Select all
<?php
$path = 'logs/';
$mydir = dir($path);
$files = array();
while(($file = $mydir->read()) !== false) {
if($file !== "." && $file !== "..") {
$files[] = $file;
}
}
$mydir->close();
$start = (isSet($GET['start'])) ? $_GET['start'] : 0;
$num_files = count($files);
for ($i = $start; ($i < ($start + 10)) && ($i < $num_files); $i++) {
echo ""; // links
}
?>
<a href="threads.php?start=0">0-10</a>
<a href="threads.php?start=10">10-20</a>
etc....
oh and btw- kettle_drum has a very nice class for doing this type of stuff. see his signature.
mairaj
Forum Newbie
Posts: 11 Joined: Mon Sep 20, 2004 2:40 am
Post
by mairaj » Wed Nov 10, 2004 10:49 pm
Hi,
Thanks so much for your reply rehfeld, but the code doesn't seem to work properly. It doesn't show the next 10 file names when clicked 'Next' but the same. Below is what I am using...
Code: Select all
<?
$path = 'logs/';
$mydir = dir($path);
$files = array();
while(($file = $mydir->read()) !== false) {
if($file !== "." && $file !== "..") {
$filesї] = $file;
}
}
$mydir->close();
$start = (isset($GETї'start']))?$_GETї'start']:0;
$num_files = count($files);
for ($i = $start; ($i < ($start + 10)) && ($i < $num_files); $i++) {
echo "<a href='readfile.php?filename=$path$filesї$i]'><font face='verdana' size='2' color='#a00000'>$filesї$i]</font><br>";
}
$begin = $i + 1;
$end = $i + 10;
echo "<a href='list.php?start=$i'>Next ї$begin - $end]</a>";
?>
Any ideas?
Thanks once again for the help!
Oh BTW please check that in running here:
http://takeoutmadeeasy.com/test/andy/files/list.php
Mairaj.
rehfeld
Forum Regular
Posts: 741 Joined: Mon Oct 18, 2004 8:14 pm
Post
by rehfeld » Thu Nov 11, 2004 12:37 am
i made a typo
change
$start = (isset($GET['start']))?$_GET['start']:0;
to
$start = (isset($_GET['start']))?$_GET['start']:0;
mairaj
Forum Newbie
Posts: 11 Joined: Mon Sep 20, 2004 2:40 am
Post
by mairaj » Fri Nov 12, 2004 1:44 pm
O yeah! I too didn't figure that out. Thanks soooo much for all your help! The code works perfectly now...
Thanks once again!