php pagination to foreach

hello,

I created small script with foreach loop. This script is use for to search files in directory. Everything is working perfectly, now I want to add pagination so that it can split display i.e.: 1,2,3 NEXT >>… Here is my code:

[php]

<?php function ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } ?> <?php function count_deep($folder, $filetype = "*", $count_folders = false) { $c = 0; $dirs = array($folder); while($dir = each($dirs)) { foreach(glob($dir[1]."/*", GLOB_ONLYDIR) as $filename) { $dirs[] = $filename; } $c += count(glob($dir[1]."/".$filetype)); } if(!$count_folders && ($filetype == "*")) $c -= (count($dirs)-1); return $c; } echo "
"; echo count_deep("backup"); echo "
" ; echo $_SESSION['calc'] ; echo "

"; $i = 1; foreach (ListFiles('backup') as $key=>$file){ $num= $i++; echo "
$num</strong $file (". date ("F d Y H:i:s", filemtime($file)) ; echo ") " . humanSize(filesize($file)) ; echo "
"; $calc = $calc + (filesize($file)) ; [b] HOW can I ADD pagination here ??[/b] } } ?>

[/php]

So, you create an array called $files and you want to show 3 of them?

Do you mean you want 4 links somewhere and the first 3 are live filenames and the fourth is next.
And, each link sends the user somewhere? Or are you talking about displaying 3 actual files that are
selected or passed to something?

More info and we can help…

Thanks

This is fairly simple. I will give you the general way to do this and then, once you get some code, display it and we will help you finish it up…

Usually, for something like this you would keep a variable that would be the current page you are on.
Let’s call it $currentpage to keep it simple. Most will display the $files in rows and columns. I usually use
tables to make them all uniform. So, something like this would display the first 25 files in grid of 5 x 5…

$currentpage = 0; // this is really an index into the files array by 25’s, 0, 25, 50, etc…
echo “

”;
for ($i = 0; $i <= 4; $i++) {
echo “”;
for ($j = 0; $j <= 4; $j++) {
echo “”;
}
echo “”;
}
echo “
” . $files[$currentpage + $i * 5 + $j] . “
”;

Now, we have 25 files displayed in the grid… Next, show links for the next 3 pages… (up to you how many!)
Let’s say you call this page that displays the files as “selectfile.php”. The first thing it should do is pull the starting file that you want to start with. Above we started with zero. You can do this in several ways, but the easiest is just to pass it with arguments. So, at the beginning of “selectfile.php” you would use:
$currentpage = $_GET[‘page’];
if($currentpage == “”){ $currentpage=0 }; //if no page passed, set to zero…
Then, you know the current page and you can use the links to the next page of files as an argument such as in this way: page=0 page=25 page=50 And, to display three links to the next pages like this:
echo “2”;
echo “3”;
echo “4”;
So, if you are on page #1 (which starts at 0), these would pass on links to page 2,3,4 at files#25, 50, 75
If one link is clicked on, it sends the user to the same page, but, the PHP code displays different data in table.

Of course, you have to keep tabs on the links. Let’s say you show 9 links. The middle one would be the
one that you want to show in a different color to indicate that it is the one you are on. That is not too hard
either, but we can talk about that later.
So, if we are on page 37 it would be: 33 34 35 36 37 38 39 40 41
You would have to keep if the current page is less than 9 to start at page one. Easy to sort out…

So, that is one basic way to do this. There are other ways so that the page numbers are hidden, but,
usually they are not needed as most do not care how the code get’s them to where they want to be.
Hope that helps!

Sponsor our Newsletter | Privacy Policy | Terms of Service