Hi all,,

I have created an front end which will display the list of items.....if the list of items exceeds ,
it elongates the page, but i want to limit it for the first page, if more number of items then it should be possible to display in the next page........how to do this

Dani AI

Generated

For : the problem is pagination — as noted — and provided a working LIMIT/OFFSET example. Below are practical, up-to-date recommendations and an alternative pattern that avoids copying the posted code but makes the solution safer and more scalable.

Use a modern DB API (PDO or mysqli) and always validate/sanitize paging inputs (cast to int, enforce min/max). Let users choose a sensible per-page cap (for example 5–100) to avoid huge responses. Escape any output for HTML to prevent XSS. Decide early whether you need numbered pages (page 1, 2, 3) or a sequential “load more” flow — that choice affects which pagination method to use.

For large tables, prefer keyset (cursor) pagination instead of offset-based LIMIT with big offsets. Keyset is much faster and uses an indexed column (usually the primary key or timestamp). It’s ideal for “load more” or next/previous flows; it does not easily support jumping to arbitrary page numbers. Example (PDO, cursor-based):

$perPage = 20;
$after = isset($_GET['after']) ? (int) $_GET['after'] : 0;

$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

$stmt = $pdo->prepare('SELECT id, title, created_at FROM items WHERE id > :after ORDER BY id ASC LIMIT :limit');
$stmt->bindValue(':after', $after, PDO::PARAM_INT);
$stmt->bindValue(':limit', $perPage, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$nextCursor = null;
if (count($rows)) {
    $nextCursor = $rows[count($rows) - 1]['id'];
}
// build next link like: ?after=<?= $nextCursor ?>

If you need jump-to-page functionality, use offset-based paging but add indexed ORDER BY columns, validate page numbers, and avoid frequent COUNT(*) on huge tables (cache totals or update via background jobs). For SEO and UX: keep consistent URLs, add rel="prev"/rel="next" when appropriate, and handle out-of-range pages by redirecting to the last available page or returning a friendly message.

Recommended Answers

All 2 Replies

this is the code for paging:

<?php
 mysql_connect("localhost", "root", "1234");
 mysql_select_db("image");

// how many rows to show per page
$rowsPerPage = 20;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$query  = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

// print the random numbers
while($row = mysql_fetch_array($result))
{
	echo $row['val'] . '<br>';
}
echo '<br>';

// how many rows we have in database
$query   = "SELECT COUNT(val) AS numrows FROM randoms";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
	if ($page == $pageNum)
	{
		$nav .= " $page ";   // no need to create a link to current page
	}
	else
	{
		$nav .= " <a href=\"$self?page=$page\">$page</a> ";
	}		
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
	$page = $pageNum - 1;
	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
	
	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
} 
else
{
	$prev  = '&nbsp;'; // we're on page one, don't print previous link
	$first = '&nbsp;'; // nor the first page link
}

if ($pageNum < $maxPage)
{
	$page = $pageNum + 1;
	$next = " <a href=\"$self?page=$page\">[Next]</a> ";
	
	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} 
else
{
	$next = '&nbsp;'; // we're on the last page, don't print next link
	$last = '&nbsp;'; // nor the last page link
}

// print the navigation link
echo $first . $prev . $nav . $next . $last;

// and close the database connection

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.