Jump to content

tabling and pagination


aleX_hill

Recommended Posts

Can someone point me in the direction of a script that will read the contents of a folder (they are all images) and then put them in a table, with pagination? I have a script that will read the files and put them in a table, but dont know how to start from half way through the folder on page 2 etc (without first making x readdir requests).

 

<?php 

    // Type in the full / absolute path to the folder... 
    $path = $_SERVER[ 'DOCUMENT_ROOT' ] . "/clients/client1/"; 

    // This opens the folder... 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 
?>
<table><tr>
<?php
$i = 0;
    // This loops through the files... 
    while ($file = readdir($dir_handle)) {
 if($i == 4)
 {
 	$i = 0;
	echo "</tr><tr>";
 }
    if($file == "." || $file == ".." || $file == "index.php" ) 
        continue; 
        echo "<td><img src='client1/$file' height='400'></td>"; 
	$i++;
    } 
?>
     </tr></table>
<?php
    // Close it... 
    closedir($dir_handle); 

?> 

 

Any help is greatly appreciated.

 

Alex

Link to comment
https://forums.phpfreaks.com/topic/97201-tabling-and-pagination/
Share on other sites

You'll want to read all the filenames into an array. Then have a variable in the URL, like $_GET['start']. That will be index of the filename array that you'll start on. Then print the next ten after that (or however many you want per page).

 

$files_per_page = 10;
$files = scandir($path);

for ($file_count = 0; $file_count < count($files); $file_count++) {
     if ($file_count >= $_GET['start'] && $file_count <= $_GET['start']+$files_per_page) {
          // You'd print the file here
     }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.