Page 1 of 1

listing file names from a directory -- 10 at a time???

Posted: Wed Nov 10, 2004 3:43 pm
by mairaj
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>";
&#125;
&#125;

$mydir->close();
In simple words, I want to limit the display of filenames from a directory to 10. Can anybody please help me???

Cheers.

Posted: Wed Nov 10, 2004 4:24 pm
by rehfeld
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.

Posted: Wed Nov 10, 2004 10:49 pm
by mairaj
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) &#123;
		if($file !== "." && $file !== "..") &#123;
		$files&#1111;] = $file;
		&#125;
	&#125;

	$mydir->close();

	$start = (isset($GET&#1111;'start']))?$_GET&#1111;'start']:0;
	$num_files = count($files);

	for ($i = $start; ($i < ($start + 10)) && ($i < $num_files); $i++) &#123;
	echo "<a href='readfile.php?filename=$path$files&#1111;$i]'><font face='verdana' size='2' color='#a00000'>$files&#1111;$i]</font><br>";
	&#125;

$begin = $i + 1;
$end = $i + 10;
echo "<a href='list.php?start=$i'>Next &#1111;$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.

Posted: Thu Nov 11, 2004 12:37 am
by rehfeld
i made a typo


change

$start = (isset($GET['start']))?$_GET['start']:0;

to


$start = (isset($_GET['start']))?$_GET['start']:0;

Posted: Fri Nov 12, 2004 1:44 pm
by mairaj
O yeah! I too didn't figure that out. Thanks soooo much for all your help! The code works perfectly now...

Thanks once again!