Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pagination without mysql

Status
Not open for further replies.

captlid

Technical User
Oct 27, 2004
82
US
say theres a listing of files in a directory, and you want to list 12 files per page...
Code:
if ($handle = opendir('./dir')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "<a href=\"$file\">$file</a><br>" ;

        }
	$totalfiles += count($file);
    }
    closedir($handle);
}
I got the files to display and the amount of files in the dir.

Code:
$filesperpage = 12;

//rounds off to higher value so remainder is shown
$totalpages = ceil($totalfiles/$filesperpage); 
echo $totalpages . ' ';

// prints page links...
for($j = 1; $j <= $totalpages; )  
{print "<a href=\"$j\">" . $j++ . ' </a>'; }

What I dont understand how to do is split the total amount of files to output 12 file links per page, and then the next 12 on the second page etc... (assuming the system sorts by filename in ascending order)

thanks,

 
I think the easiest way would be a nested loop:

for ($j = 0; $j < $totalpages; $j++)
{
for ($i = $j * 12; $i < $j * 12 + 12; $i++)
{
<print file info>
}
}

 
While I haven't tried it myself, I think using a session variable would be a good solution.

If the session variable doesn't exist, then your script can get the directory information, put it in an array, sort it if needed, then set the session variable.

If it exists already, all you need to do is count the elements of the array, and figure out where you should start displaying, then only echo x entries. I'd put more together regarding this, but right now I don't have the time.
 
$picsperpage = 12;
$startpos = $page * $picsperpage;
$endpos = ($page * $picsperpage) + $picsperpage;

if ($handle = opendir('./dir')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "thumbs") {

for ($i = $startpos; $i < $endpos; $i++)
{ echo "<a href=\"$file\"><img src=\"$file\" width=\"50\" height=\"50\" alt=\"$file\"></a>" ; }
}
$totalfiles += count($file);
}
closedir($handle);
}


$totalpages = ceil($totalfiles/$picsperpage);
$page = $_GET['page'];
if (!$page) {$page = 1;}

//page generation
for ($j = 1; $j <= $totalpages; $j++)
{echo "<a href=\"readdir.php?page=$j\"> $j </a> ";}

I tried this, but it outputs 12 copies of each file!!!!
 
how about put the fiels in an assocciative array and give them each a unique id. after you output the first 12 in the first page, remove these 12 elements from the array, and so on...
 
well i got the script working. It still has 3 bugs in it though. :(
1. when the $page = null or !$page it outputs negative values...
i.e. (should output the same values as 2. On the last page if theres less than 12 images, it outputs the proper amount of images, but it still adds placeholders for the rest. (i.e. if theres 6 images left over, it will output 6 more empty placeholders)
3. I am not sure why it counts the . and .. and the one non image file in the directory, even though I specified not to include those three things in the dir being scanned.

Code:
$picsperpage = 12;
$startpos = ($page-1) * $picsperpage;
$endpos = (($page-1) * $picsperpage) + $picsperpage;

if ($handle = opendir('./dir')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && $file != "index.php")
        { $filea[] = $file; sort($filea);  }

  $totalfiles += count($file);

  }
    closedir($handle);
}

$dir = "dir/";
echo '<div style="width:800px">';

 for ($i = $startpos; $i < $endpos; $i++)
{  print $i+1;  print "<a href=\"$dir$filea[$i]\">
<img src=\"$dir$filea[$i]\" width=\"160\" height=\"120\" alt=\"$file\" border=\"1\">
</a>" ; }

$page = $_GET['page']; if ($page == null) { $page = 1  ;}
	$totalpages = ceil(($totalfiles)/$picsperpage);
	for ($j = 1; $j <= $totalpages; $j++) { echo "<a href=\"readdir.php?page=$j\">Page $j </a> "; }
 
well i fixed the first error, apparently the $_get['page'] = $page;
should be before the calculations for start and end position...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top