Page 1 of 1

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

Posted: Fri Sep 14, 2007 9:02 pm
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>";
            }           
            ?>

Posted: Fri Sep 14, 2007 9:06 pm
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.

Posted: Fri Sep 14, 2007 10:18 pm
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.

check out this one...

Posted: Sat Sep 15, 2007 12:08 am
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 */
}
?>

Posted: Sat Sep 15, 2007 12:08 am
by Christopher
lafflin's question:

viewtopic.php?t=73598

Previous pagination discussion:

viewtopic.php?t=43777&start=30