How to paginate like "Page: 1,2,3,4,5,6,..."

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
achintha
Forum Newbie
Posts: 13
Joined: Mon Apr 30, 2007 10:21 am

How to paginate like "Page: 1,2,3,4,5,6,..."

Post by achintha »

currently I'm using pagination.but it's only have "Previous page | Next page"
but i want to dispaly it like Page: 1,2,3,4,5,6,... I attahc the code currently i using.pls help me

Code: Select all

<?php
            $pagenext = $page+1;
            $result1 = $db->getnewsbycatid($pagenext,$front_latestoncatarecord,$catalogid);
            if ($page!=0)
            {
            $pagepre = $page-1;         
            print "<a href=\"$PHP_SELF?page=$pagepre&catalogid=$catalogid\" class=\"en_b\">$front_previouspage</a>&nbsp;";
            }
            if (!empty($result1))
            {         
            print "| <a href=\"$PHP_SELF?page=$pagenext&catalogid=$catalogid\" class=\"en_b\">$front_nextpage</a>";
            }           
            ?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

A for() loop.

Something like this:

Code: Select all

$nextPages = $currentPage + 5; //show five numbers

for ($i = $currentPage; $i < $nextPages; $i++)
{
     echo '<a href="page.php?page=' . $i . '">' . $i . '</a>';
}
You should search this forum for a pagination class.. some good ones have been posted.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
lafflin
Forum Contributor
Posts: 123
Joined: Thu Jul 26, 2007 6:26 pm

Post by lafflin »

He's right. I just posted about the subject a day or two ago and recieved some great input on the subject. The tricky part is trying to maintain the same query results if the query is a multiple field search query after clicking a pagination link. That's what my post was about. There's a few ways to do it, what I believe to be the easiest method is posted in a discussion a few days old, just search my threads by name and you'll find it.
navindhar
Forum Newbie
Posts: 6
Joined: Fri Sep 14, 2007 10:59 am

check out this one...

Post by navindhar »

***** PLEASE USE [PHP] AND OTHER TAGS WHEN POSTING CODE *****

Code: Select all

<?
/* Set current, prev and next page */
$page = (!isset($_GET['page'])? 1 : 0;
$prev = ($page - 1);
$next = ($page + 1);

/* Max results per page */
$max_results = 5;

/* Calculate the offset */
$from = (($page * $max_results) - $max_results);

/* Query the db for total results. You need to edit the sql to fit your needs */
$result = mysql_query("select id from tablename");

$total_results = mysql_num_rows($result);

$total_pages = ceil($total_results / $max_results);

$pagination = '';

/* Create a PREV link if there is one */
if($page > 1)
{
$pagination .= '<a href="index.php?page='.$prev.'">Previous</a> ';
}

/* Loop through the total pages */
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
$pagination .= $i;
}
else
{
$pagination .= '<a href="index.php?page='.$i.'">$i</a>';
}
}

/* Print NEXT link if there is one */
if($page < $total_pages)
{
$pagination .= '<a href="index.php?page='.$next.'">Next</a>";
}

/* Now we have our pagination links in a variable($pagination) ready to print to the page. I pu it in a variable because you may want to show them at the top and bottom of the page */

/* Below is how you query the db for ONLY the results for the current page */
$result=mysql_query("select *
from tablename
LIMIT $from, $max_results ");

while ($i = mysql_fetch_array($result))
{
/* This is where you print your current results to the page */
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

lafflin's question:

viewtopic.php?t=73598

Previous pagination discussion:

viewtopic.php?t=43777&start=30
(#10850)
Post Reply