PHP LIST DIRECTORY AND PAGINATION CODE SAMPLE

PHP LIST DIRECTORY AND PAGINATION CODE SAMPLE

<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title><?php echo gethostname().”-PROJECTS”; ?></title>
</head>

<body>

<?php

$folders = glob(“*”, GLOB_ONLYDIR);
usort($folders, function ($a, $b) {
return filemtime($b) – filemtime($a);
});

$record_count  = 5;
$total_pages   = ceil(count($folders)/$record_count);
$page          = isset($_REQUEST[‘page’])?$_REQUEST[‘page’]:1; ///make it dyanamic :: page num
$offset        = ($page-1)*$record_count;
$folders_filter  = array_slice($folders, $offset,$record_count);

foreach ($folders_filter as $folder) {
echo “<a href=’$folder’>$folder</a> <br/>”;
}

if($total_pages > 1){
if($page != 1){
echo ‘<a href=”index.php?page=’.($page-1).'”>Prev</a>’;
}
if($page != $total_pages){
echo ‘<a href=”index.php?page=’.($page+1).'”>Next</a>’;
}
}
?>

</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s