This is a pagination code that I used in one of my old applications. You should change the mysql_ functions to pdo/mysqli here. This is based on a simple table with id, name fields. Change the queries according to your logic.
if (isset($_GET['page'])){
$page = $_GET['page'];
}
// get total no. of rows from the table
$page_query = "SELECT COUNT(*) as C FROM table";
$RSp = MYSQL_QUERY($page_query) or die(mysql_error());
$rowp = mysql_fetch_array($RSp, MYSQL_ASSOC);
$num_rows = $rowp['C'];
$rows_per_page = 100; // you can change this to what you want
$first = 1;
$last = ceil ($num_rows/$rows_per_page);
if (($page == '') || ($page < $first) || ($page > $last)){
$page = 1;
}
$lower_limit = (($page-1)*$rows_per_page);
$count = 0;
// main query to generate page content based on
// pagination logic
$query = "Select * from table order by name ";
$query.= "LIMIT ".$lower_limit.", ".$rows_per_page;
// generate page content here
// pagination code at the bottom of the page goes here
<!--This is the main pagination HTML Code-->
<ul id="pagination-clean">
<?php
if ($page == 1){
?>
<li class="previous-off">Previous</li>
<?php
}
else {
?>
<li class="previous"><a href="?page=<?php echo ($page-1); ?>">Previous</a></li>
<?php
} // end else
for ($i = 1; $i<=$last;$i++){
// This is the query statement for displaying the name of
// first and last entry of the page
// on paginated links
$lower = (($i-1) * $rows_per_page);
$upper = (($i*$rows_per_page) - 1);
$query_low = "SELECT Name, SUBSTRING(Name, 1, 2) AS `Name_S` FROM ";
$query_low.= "table ORDER BY Name ASC LIMIT ".$lower.", 1";
$RSl = MYSQL_QUERY($query_low) or die(mysql_error());
$row_low = mysql_fetch_array($RSl, MYSQL_ASSOC);
$lname = $row_low['Name'];
$lname_s = $row_low['Name_S'];
$query_up = "SELECT Name, SUBSTRING(Name, 1, 2) AS `Name_S` FROM ";
$query_up.= "table ORDER BY Name ASC LIMIT ".$upper.", 1";
$RSu = MYSQL_QUERY($query_up) or die(mysql_error());
$row_up = mysql_fetch_array($RSu, MYSQL_ASSOC);
if (!$row_up){
$query_up = "SELECT Name, SUBSTRING(Name, 1, 2) AS `Name_S` FROM ";
$query_up.= "table ORDER BY Name ASC LIMIT ".($num_rows-1).", 1";
$RSu = MYSQL_QUERY($query_up) or die(mysql_error());
$row_up = mysql_fetch_array($RSu, MYSQL_ASSOC);
}
$uname = $row_up['Name'];
$uname_s = $row_up['Name_S'];
if ($i == $page){
?>
<li class="previous-off"><?php echo $i ." (".$lname_s." - ".$uname_s.")"; ?></li>
<?php
}
else {
?>
<li><a href="?page=<?php echo $i; ?>" ><?php echo $i ." (".$lname_s." - ".$uname_s.")"; ?></a></li>
<?php
}
} // end for loop
if ($page == $last){
?>
<li class="next-off">Next</li>
<?php
}
else {
?>
<li class="next"><a href="?page=<?PHP echo ($page+1)?>">Next</a></li>
<?php
}
?>
</ul>
<!-- end pagination code here -->
This is the CSS:
#pagination-clean li { border:0; margin:0; padding:0; font-size:11px; list-style:none; /* savers */ float:left; }
/* savers #pagination-clean li,*/
#pagination-clean a { border-right:solid 1px #DEDEDE; margin-right:2px; }
#pagination-clean .previous-off,
#pagination-clean .next-off { color:#888888; display:block; float:left; font-weight:bold; padding:3px 4px; }
#pagination-clean .next a,
#pagination-clean previous a { border:none; font-weight:bold; }
#pagination-clean .active { color:#000000; font-weight:bold; display:block; float:left; padding:4px 6px; /* savers */ border-right:solid 1px #DEDEDE; }
#pagination-clean a:link,
#pagination-clean a:visited { color:#0e509e; display:block; float:left; padding:3px 6px; text-decoration:underline; }
#pagination-clean a:hover { text-decoration:none;
This is a screenshot of what the page looks like:
https://i.stack.imgur.com/3px7n.jpg
(In addition to the page no., it also displays the first 2 characters of the name field)
See if this helps. Let me know if you have any questions.