Jump to content

Pagination navigation based on pages in directory


pbrowne

Recommended Posts

The code below reads the contents og a directory which contains html pages named numerically (1.html, 2.html etc).  It then creates a horizontal list of numbered links and a previous/next link (similar to the Google pagination) based on the number of pages in the pages directory.

 

This was working OK, but the pages were not linking to the corresponding link number, it appeared to be random (but not really).  What was happening is the the readdir ws reading the files as they were sorted on the unix directory, not alphabetically or numerically.

 

I have since added sorting with sort($sorted_pages, SORT_NUMERIC); and then attempting to loop through this array with for($i=0;$i<count($sorted_pages);$i++){ $tn = $sorted_pages[$i];

 

I'm sure it's getting closer but still not working.  You can test this with the code below and creating a directory names 'pages' containing some numbered html pages...

 

Any help appreciated!!!! :P

 

 

<?
    ?>
        <html>
            <body>
    <? 
    $page = 0;  
    // I guess we want the page in an iframe or something?
    $frame = "";
    $list = ""; 
    if($_GET['page']) {
        $page = $_GET['page'];
    } 
    if($page > 0) {
        $list .= "<a href=\"page.php?page=".($page-1)."\">Previous</a> ";
    } else {
        $list .= "Previous ";
    } 
    if ($dir = opendir('pages')) {
        $count = 0; 
while ($file = readdir($dir)) {

            if($file == "." || $file == "..") {
                continue;
            }
            if($page == $count) {
			$sorted_pages = array();
			$sorted_pages[] = $file; 
			sort($sorted_pages, SORT_NUMERIC);
			for($i=0;$i<count($sorted_pages);$i++){
				$tn = $sorted_pages[$i];
				echo $tn . '\n';

                // View it in a frame?
                $frame = " <iframe width='640' height='480' src=\"pages/".$tn."\"></iframe><br />\n";
                $list .= " ".$count." ";
			}
            } else {
                //$list .= " <a href=\"page.php?page=".$count."\">".$count."</a> ";
$list .= " <a href=\"page.php?page=".$tn."\">".$count."</a> ";
            }
            $count++;
        }
    }

    if($page < $count-1) {
        $list .= " <a href=\"page.php?page=".($page+1)."\">Next</a> ";
    } else {
        $list .= " Next";
    }    
    echo $frame;
    echo $list; 
    ?>
            </body>
        </html>
    <?
?>

 

OK, the code below sort of works.  Two problems:

 

1.  The linked pages need to show on the same page as the nav links, i.e. above and possibly in an iframe.

 

2. The link to page 1 doesn't work, neither does the link to page 9, which throws out all the links after the link to page 9.

 

Getting close...any suggestions?

 

Thanks,

 

Peter

 

<?php
function dirList ($directory){

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {
    sort($results, SORT_NUMERIC);
        // if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != '.' && $file != '..')
            $results[] = $file;
    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}

$directory = 'pages';

$dirarray = (dirList($directory));

$curr = 1;
$nav = $list = $prev = $next = '';

foreach ($dirarray as $count => $file) {
      $index = $count+1;
      
      if ($curr == $index) {
            $prevCount = $index-1;
            $nextCount = $index+1;
            $prev = '<a href=' . $directory . '/' . $dirarray[$count] . '>Previous</a>';
            $next = '<a href=' . $directory . '/' . $dirarray[$nextCount] . '>Next</a>';
            $list .= "$index ";
      }else{
            $list .= '<a href=' . $directory . '/' . $file . '>' . $index . '</a>';
}
}
$nav = $prev.$list.$next;
echo 'Current = ' . $curr . '<br>';
echo $nav;

?>

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.