Code: Select all
<?php
$image_url = 'images/';
//User defined variables for page settings
$rows_per_page = 2;
$cols_per_page = 4;
//Master array of ALL the images in the order to be displayed
$images = array(
'image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg',
'image5.jpg', 'image6.jpg', 'image7.jpg', 'image8.jpg',
'image9.jpg', 'image10.jpg', 'image11.jpg', 'image12.jpg',
'image13.jpg', 'image14.jpg', 'image15.jpg', 'image16.jpg',
'image17.jpg', 'image18.jpg', 'image19.jpg'
);
//END USER DEFINED VARIABLES
//System defined variable
$records_per_page = $rows_per_page * $cols_per_page;
$total_records = count($images);
$total_pages = ceil($total_records / $records_per_page);
//Get/define current page
$current_page = (int) $_GET['page'];
if($current_page<1 || $current_page>$total_pages)
{
$current_page = 1;
}
//Get records for the current page
$page_images = array_splice($images, ($current_page-1)*$records_per_page, $records_per_page);
//Create ouput for the records of the current page
$ouput = "<table border=\"1\">\n";
for($row=0; $row<$rows_per_page; $row++)
{
$ouput .= "<tr>\n";
for($col=0; $col<$cols_per_page; $col++)
{
$imgIdx = ($row * $rows_per_page) + $col;
$img = (isset($page_images[$imgIdx])) ? "<img src=\"{$image_url}{$page_images[$imgIdx]}\" />" : ' ';
$ouput .= "<td>$img</td>\n";
}
$ouput .= "</tr>\n";
}
$ouput .= "</table>";
//Create pagination links
$first = "First";
$prev = "Prev";
$next = "Next";
$last = "Last";
if($current_page>1)
{
$prevPage = $current_page - 1;
$first = "<a href=\"test.php?page=1\">First</a>";
$prev = "<a href=\"test.php?page={$prevPage}\">Prev</a>";
}
if($current_page<$total_pages)
{
$nextPage = $current_page + 1;
$next = "<a href=\"test.php?page={$nextPage}\">Next</a>";
$last = "<a href=\"test.php?page={$total_pages}\">Last</a>";
}
?>
<html>
<body>
<h2>Here are the records for page <?php echo $current_page; ?></h2>
<ul>
<?php echo $ouput; ?>
</ul>
Page <?php echo $current_page; ?> of <?php echo $total_pages; ?>
<br />
<?php echo "{$first} | {$prev} | {$next} | {$last}"; ?>
</body>
</html>
Also, for some reason, pagination is not working. Anyone care to do a diagnosis?
Thanks!