for example, the blogs, how they divide the entries in pages?

i have kinda of a idea like if ur on page 2 and ur blog shows 5 post per page the $row=mysql_fetch_assoc would execute 5 times before starting to post, thats the only way i can think about, is there any other easy way to do it?

Dani AI

Generated

This is pagination — named it correctly. The idea of fetching all rows and looping to skip N is wasteful for anything but tiny tables. Use the SQL LIMIT/OFFSET clause so the database returns only the slice needed for the current page. 's single-page or AJAX idea is fine for the UI; the backend query should still be paged.

Example (safe, server-side pagination using PDO):

$page   = max(1, (int)($_GET['page'] ?? 1));
$per    = 5;
$offset = ($page - 1) * $per;

$stmt = $pdo->prepare('SELECT id,title,body FROM posts ORDER BY created_at DESC LIMIT ? OFFSET ?');
$stmt->bindValue(1, $per, PDO::PARAM_INT);
$stmt->bindValue(2, $offset, PDO::PARAM_INT);
$stmt->execute();
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);

$total    = (int)$pdo->query('SELECT COUNT(*) FROM posts')->fetchColumn();
$totalPages = (int)ceil($total / $per);

Notes and tips: validate and cast the incoming page parameter so offsets are nonnegative; always include an ORDER BY for stable paging; use prepared statements (PDO or mysqli) instead of the old mysql_ functions; compute total pages with `SELECT COUNT()` when showing page numbers. For very large tables, consider keyset (seek) pagination (WHERE id < last_id ORDER BY id DESC LIMIT N) to avoid costly large offsets. See the SQL LIMIT docs and PDO prepare reference for details: MySQL SELECT / LIMIT and PDO::prepare.

Recommended Answers

All 4 Replies

hi
i did not get that actually what you want to do.
means u want to display the blog and also want to give functionality to user for update and add new blogs right.

so you can do all this thing on one page just u need to call another page when user click on update blog or add new blog or you can call this page by the ajax also.


Thanks

hmm i know my explanation wasnt the best, what i meant was the classic next page button that shows the next X record from a database

Its called pagination.

Do a google search and it should provide you with everything you need.

hi
yes this called pagination this code you can find from the google serach else i give you because this all thing i did in my site ok

Thanks

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.