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

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
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

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

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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.
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

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 »

O yeah! I too didn't figure that out. Thanks soooo much for all your help! The code works perfectly now...

Thanks once again!
Post Reply