I still don't see why you can't use pagination for this. Clicking an image isn't the same as stepping through the pagination. Pagination always moves you N items backwards or forwards in the set. Clicking an image doesn't change where in the pagination you are, but may very well change the currently displayed content (enlargement of clicked image, the point in time your start watching in a movie etc). If you want to always step through the pagination when a new image is clicked, so that the currently viewed image is always in the center of the pagination navigation, see the second code snippet. But do note the similarities between the two pagination versions.
Untested and likely to contain several issues. It wouldn't surprise me if end conditions doesn't work, for example when viewing first or last page in paginated set.
if (!isset($_GET['page']))
{
$page = 1;
}
else
{
$page = (int) $_GET['page'];
if ($page < 1)
{
} $page = 1;
}
$itemsPerPage = 10;
$offset = ($page - 1) * $itemsPerPage;
$qry = sprintf('SELECT fields FROM table WHERE condition ORDER BY order LIMIT %d, %d',
$offset, $itemsPerPage;
# Same as above query without order and limit clauses. count(*) instead of fields
$itemCountQry = sprintf('SELECT count(*) AS item_count FROM table WHERE condition')
/* ... execute query etc ... */
/* Show images - completely separate from pagination issue. */
foreach ($result as $image)
{
printf('<img ... id="%d"/>', $image['id']);
}
/* pagination */
$from = $page - 3;
# Special condition: current page is among the first 3 pages in the set.
if ($from < 1)
{
$from = 1;
}
$to = $from + 7;
$maxPages = ceil($itemCount / $itemsPerPage);
# Special condition - current page is among the last 3 pages in the set
if ($to > $maxPages)
{
$to = $maxPages;
$from = $to - 7;
# Special condition: fewer than 7 pages in the set.
if ($from < 1)
{
$from = 1;
}
}
# Output pagination
for ($i = $from; $i <= $to; ++$i)
{
printf('<a href="?%s" class="%s">%d</a>',
urlencode('page='.$page),
($i = $page ? 'bold' : ''),
$i);
}
But if you want to always have the currently viewed image centered in the presented subset, it's still pagination, just with very minor changes from the above code - the concept is identical
if (!isset($_GET['image']))
{
$image = 1;
}
else
{
$image = (int) $_GET['image'];
if ($image < 1)
{
} $image = 1;
}
$itemsPerPage = 7;
# Here is how you specify 3 images before current image
$offset = ($image - 1) - 3;
if ($offset < 0)
$offset = 0;
# Now you need to know the number of total items
$itemCountQry = sprintf('SELECT count(*) AS item_count FROM table WHERE condition');
/* ... fetch result row ...*/
if ($offset + $itemsPerPage > $row['image_count'])
{
$offset = $row['image_count'] - 7;
if ($offset < 0)
$offset = 0;
}
$qry = sprintf('SELECT fields FROM table WHERE condition ORDER BY order LIMIT %d, %d',
$offset, $itemsPerPage;
/* Show images - now also used for pagination */
foreach ($result as $img)
{
printf(
'<a href="?%s">
<img ... id="%d" />
</a>',
urlencode('image='.++$offset),
$img['id']);
}