954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Simple (but powerful) paging function

By Gewalop on Mar 4th, 2011 4:11 pm

Hi,
- Why use paging?
It's not really cool to print out all 2000 rows you have in a table to the page, so it'd be nice to page them.

Special cases?
Yeah, if you have 1000 rows.. and you've set the maximum row per page to 20, well.. you're gonna get 50 links for your pages.. so I took care of that. How? I only show the first costume number of page links, and the more you navigate, the more pages you'll see.

Parameters:
1- The maximum number of results you want in each page.
2- The maximum number of pages links you want in the page.
3- The query you've used to get your results.
4- The page that has your results.


Example (call the function in the place you want to put the page links)

<?php
pageThis(20, 10, "SELECT * FROM notes", notes.php);
?>
//Preparing --Start--
function pageThis($max, $max_page_links, $query, $page_link){
//Uncomment the next 4 lines if don't wish to use this as a function
//$page_link="paged.php";
//$max=30;//Maximum items per page (rows)
//$max_page_links=10;//Maximum links number in the page
//$query="select * from some_table";//Your query
$num_stuff=mysql_num_rows(mysql_query($query));//Getting the total possible rows
if (isset($_GET['page'])&&$_GET['page']>0){
    $start=intval($_GET['page'])-1;//Now the page number make more sense (page=1 is actually the first page)
    $current_page=intval($_GET['page']);//Some cleaning (SQL Injection prevention, and get rid of negative numbers)
}
//If no parameters passed.. just give the first page
else {
    $current_page=1;
    $start=0;
}
//If a large page numbre passed (more than there actually is) just give the last page
if ($current_page>ceil($num_stuff/$max)){
    	$current_page=ceil($num_stuff/$max);
    	$start=ceil($num_stuff/$max)-1;
}
$start*=$max;//Which row to start with
$get_stuff_query.=" limit $start, $max";//Adding the limit
//Preparing --End--


//Actual paging --Start--
if ($num_stuff>$max){//Is there any need for pagin?
	if ($current_page>1){//Making previous page & first page links when needed
                $previous_page=$current_page-1;//previousious means -1
                echo "<a style=\"text-decoration: none;\" href=\"".$page_link."page=1\">|<</a>  ";//First page is the page number.. you guessed it right.. 1
                echo "<a style=\"text-decoration: none;\" href=\"".$page_link."page=$previous_page\"<<</a>  ";
            }
            if ($current_page>$max_page_links/2){//Are we going far away from the first viewed page link?
                if (ceil($num_stuff/$max)-$current_page<(($max_page_links/2)+1)){//Are we getting closer to last viewed page link?
                    $start_counter=$current_page-($max_page_links-(ceil($num_stuff/$max)-$current_page));//Yes, Then we need to view more page links
                    $end_counter=ceil($num_stuff/$max);//And no need to view page links more than the query can bring
                }else{
                    $start_counter=$current_page-(($max_page_links/2)-1);//No, then just view some links before the currentrent page
                    $end_counter=$current_page+($max_page_links/2);//And some links after
                }
            }
            else{//Still in the first pages?
                $start_counter=1;//Start with page one
                $end_counter=$max_page_links;//Show only enough links
            }
            for ($i=$start_counter;$i<=$end_counter;$i++){//A loop for viewing the links
                if ($i==$end_counter){//Is this the last link?
                    if ($i==$current_page){//Are we actually on the last page? Because we don't need the | after the link
                        echo "<a style=\"color:blue;\" class=\"link\">".$i."</a>";//Then make it look like we're on that page
                    }else{
                        echo "<a class=\"link\" href=\"".$page_link."page=".$i."\">".$i."</a>";//Well yeah, it's the last link.. but we're not there yet.
                    }
                }else{//Not the last page you say.. mmm.. then print normally (with | after the link)
                    if ($i==$current_page){//Are we vewing this page?
                        echo "<a style=\"color:blue;\" class=\"link\">".$i."</a>  |  ";//Make us know it
                    }else{//Not viewing.. just a normal link (the most common case here)
                        echo "<a class=\"link\" href=\"".$page_link."page=".$i."\">".$i."</a>  |  ";//Nothing to say
                    }
                }
            }
            if ($current_page<ceil($num_stuff/$max)){//Making the next and last page links
                $next_page=$current_page+1;//Next means +1
                $last_page=ceil($num_stuff/$max);//and the last page is the page.. whell.. it's the last one the query can bring
                echo "  <a style=\"text-decoration: none;\" href=\"".$page_link."page=$next_page\">>></a>";
                echo "  <a style=\"text-decoration: none;\" href=\"".$page_link."page=$last_page\">>|</a>";
            }
}
}
Gewalop
Newbie Poster
17 posts since Apr 2009
Reputation Points: 11
Solved Threads: 0
 

I am testing right now. If it works for me you are my god! Thank you so much for this!!! I have been working for hours trying to get a pagination script working and here you are just spilling out the answer right in front of me. THANK YOU!

Line 33 needs to be changed from:

echo "<a style=\"text-decoration: none;\" href=\"".$page_link."page=$previous_page\"<<</a>  ";

to this... you didn't close the link:

echo "<a style=\"text-decoration: none;\" href=\"".$page_link."page=$previous_page\"><</a>  ";
epicrevolt
Junior Poster in Training
94 posts since Oct 2010
Reputation Points: 16
Solved Threads: 6
 

By the way, is there a way to limit the pagination if you have less than x amount of pages? Like if I want it to eventually display a max of 10 pages, but I only have 3 pages filled up right now, is there a way to make it only show the max if you have at least that many pages?

epicrevolt
Junior Poster in Training
94 posts since Oct 2010
Reputation Points: 16
Solved Threads: 6
 

@epicrevolt: well I've looked hard for a script that can do what I wanted, I was only able to find the first part, but I have lots of rows.. so I ended up with 120 numbered links at the end of each page.. so I decided to write one from scratch.. the minute I added this to my project I posted it, so I'm glad it was useful to you.

About your question, put the leave everything the way you want it, max rows per page and max links per page.
After the line 62 add a new "for loop" for 7 more numbers and make them linkless (no "a" tag) and you're good to go.

Gewalop
Newbie Poster
17 posts since Apr 2009
Reputation Points: 11
Solved Threads: 0
 

Could you please elaborate on the for loop? I am kinda new to PHP and I suck at for and foreach loops :(

epicrevolt
Junior Poster in Training
94 posts since Oct 2010
Reputation Points: 16
Solved Threads: 6
 

just after the line 62

<?php
for ($i=3;$i<8;$i++){
echo "<a class=\"link\" href=\"#\">".$i."</a>  |  "
}
?>

I guess it's easy

Gewalop
Newbie Poster
17 posts since Apr 2009
Reputation Points: 11
Solved Threads: 0
 

i think so u made this script quiet complicated for php beginners....
I will soon post the easier solution to this ...might be in tomorrow evening....
i have worked many times in this type of pagings...

tomato.pgn
Posting Whiz in Training
262 posts since Mar 2011
Reputation Points: 4
Solved Threads: 32
 

this is the solution to ur problem...if u want elaboration then i will explain u in another post....waiting for ur reply whether it was helpful.....

<?php
$page=1;
$nor=5;
$a=1;
if(isset($_GET['page']))
{
}
else
{
$_GET['page']=1;
}
echo $_GET['page'];
$con=mysql_connect("localhost","root","")
or die ("error");
$db=mysql_select_db("check",$con)
or die("database not found");
$result=mysql_query("SELECT * FROM comment")
or die ("query error");
echo "<table border='1'><tr><th>name</th><th>Comment</th></tr>";
$r=mysql_num_rows($result);
echo $r."<br/>";
$p=ceil($r/$nor);
//echo $p;
while($rows=mysql_fetch_array($result))
{
if($a>($_GET['page']-1)*$nor)
{
echo "<tr><td>".$rows['user']."</td><td>".$rows['comment']."</td></tr>";
}
$a=$a+1;
}
echo "</table>";
for($i=1;$i<=$p;$i++)
{
if($i==$_GET['page'])
{
echo $i;
}
else
{
echo "<a href='check.php?page=".$i."'>".$i."</a>";
}
}

?>
tomato.pgn
Posting Whiz in Training
262 posts since Mar 2011
Reputation Points: 4
Solved Threads: 32
 

if u want a more advanced paging system then i will make it for u within an hour.....just waiting what else u want frm this post.....

tomato.pgn
Posting Whiz in Training
262 posts since Mar 2011
Reputation Points: 4
Solved Threads: 32
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: