Jump to content

Help integrating pagination system


Andrei_Claudiu

Recommended Posts

So i have the readdir() and a while loop to loop through the contents like this

<?php
$directory = 'games';
$row=0;
if ($handle = opendir($directory.'/')) {
    echo '<table border="1">';
    while ($cat = readdir($handle)) {
        if ($cat!='.'&&$cat!='..'&&$cat!='index.php') {
			if($row==0) echo '<tr>';
			echo '<td align="center"><a href="'.$directory.'/'.$cat.'" style="text-decoration:none"><img src="'.$directory.'/'.$cat.'/image.php" style="display:block" />'.str_replace('_', ' ', $cat).'</a></td>';	
            if($row==5) {
              echo '</tr>';
              $row = -1;
            }
            $row++;
        }
	}
    echo '</table>';
}
?>

It also displays a table with a new row at every three columns of data

The following script is a bit harder to handle for me and its like this

$dir = "/directory_name";

// Create $files array and populate with files from $dir
$files = array();
//if (is_dir($dir)) {
  // if ($dh = opendir($dir)) {
      // while (($file = readdir($dh)) !== false) {
          // if($file != "." && $file != ".."){
               array_push($files,$file);
               //here goes the code above i think?
           // }
      // }
      // closedir($dh);
   //}
//}

// Set up some paging info
$page_size=10;
$total_pages = ceil(count($files) / $page_size);

// Get currently selected page, if not selected use 1 if too high use last page
if(isset($_GET['p'])){
    $current_page = $_GET['p'];
    if($current_page > $total_pages){
         $current_page = $total_pages;
        }
    }
    else
    {
        $current_page=1;
    }

// do some math to determine starting array index to use for the current page

$start = $current_page * $page_size - $page_size;


print "File Listing for $dir<br>Page $current_page of $total_pages<br><br>";

// Print files for current page

for($i=$start;$i<$start + $page_size;$i++){
    print "$files[$i]<br>";
}

// Print links for pages

for($j=0;$j<$total_pages;$j++){
    $p = $j+1;
     print "<a href='index.php?p=$p'>$p</a> | ";
    }  

I want to integrate the first script into the second if that's possible Thanks!

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.