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
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
     }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.