Jump to content
Killersites Forums

Pagination


Recommended Posts

I'm sure someone out there is looking for a pagination template so here it is:

 

pagination.php (class file)

 


<?php

class Pagination {

public $curr_page = 1;
public $per_page = 10; // Number if results per page if this isn't passed into the pagination class on the page...I will reference this on the actual page
public $total_count=0;

function __construct($curr_page, $per_page, $total_count) {

	$this->curr_page = (int)$curr_page;
	$this->per_page = (int)$per_page;
	$this->total_count = (int)$total_count;

}
function limit() {

	return $this->per_page;

}

function offset() {

	return $this->per_page * ($this->curr_page - 1);

}

function total_pages() {

	return ceil($this->total_count/$this->per_page);

}

function prev_pg() {

	return $this->curr_page - 1;

}

function next_pg() {

	return $this->curr_page + 1;

}

function has_prev_pg() {

	return $this->prev_pg() >= 1 ? true : false;

}

function has_next_pg() {

	return $this->next_pg() <= $this->total_pages() ? true : false;

}

} // End Pagination Class

?>

 

Then on the pages you want the pagination just add this: ***(make sure you include() or require() pagination.php on your pages)

 

Top of page:

 


// Pagination

global $db; // references my database class...you need some type of connection here for a row count in the database for $total_count

$curr_page = !empty($_GET['pg']) ? (int)$_GET['pg'] : 1; // If the pg is not set it sets $curr_page to 1

$per_page = 5; // This was set to 10 in the pagination class, but since i am passing 5 into Pagination() it will overwrite the 10.

$total_count = // You need a row count on the database you are using...this needs to equal the total rows

$pg = new Pagination($curr_page, $per_page, $total_count); // Initiates the Pagination class DON"T FORGET TO INCLUDE pagination.php ABOVE THIS LINE or it will not work

$limit = $pg->limit();   
$offset = $pg->offset();

 

This is the SQL statement and while loop for the actual data you want to retrieve from the database:

 


$sql = "SELECT * FROM TABLE_NAME LIMIT $limit OFFSET $offset"; // I'm sure most of you know MySQL already.

while($row = $db->fetch_object($result)) {

   // your table here  
}

 

This is the bottom portion:

 


<div class="pagination"> // My div for it i will post the CSS code as well.

           <?php

               $wid = $_GET['id'];

               $total = $pg->total_pages();

               if ($total > 1) {

                   if ($pg->has_prev_pg()) {

                       echo "<a href=\"index.php?id=$wid&pg="; // replace with your page URL but pg= must be included whether its ? or &
                       echo $pg->prev_pg();
                       echo "\">Previous</a>";

                   }

                   for($i=1; $i <= $total; $i++) {

                       if ($i == $curr_page) {

                           echo " <span class=\"selected\">{$i}</span> "; // In css code

                       }
                       else {
                           echo " <a href=\"index.php?id=$wid&pg={$i}\">{$i}</a> "; // replace with your page URL but pg= must be included whether its ? or &
                       }
                   }

                   if ($pg->has_next_pg()) {

                       echo "<a href=\"index.php?id=$wid&pg="; // replace with your page URL but pg= must be included whether its ? or &
                       echo $pg->next_pg();
                       echo "\">Next »</a>";

                   }


               }

               unset($limit);
               unset($offset);

           ?>    

       </div>

 

And here is the CSS:

 


.pagination {
position: relative;
width: 620px;
height: 20px;
top: 35px;
text-align: center;
vertical-align: middle;
margin: 5px auto;
}

.pagination a {
margin: 0 5px;
font-size: 1em;
color: #7E7E7E;
background: none;
}

.selected {
color: white;
font-size: 1em;
background-color: #333;
text-decoration: none;	
}

 

Obviously you can change the css to fit your needs. I hope this helps. It seems complicated at first but its just a matter of copy and paste and some DB coding. Enjoy.

 

Oh and you might want to take out everything after the //, just notes to the user :).

Edited by Archadian
Link to post
Share on other sites
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...