dbConfig.php // Database connection
1 2 3 4 5 6 7 8 9 | <?php define ("DB_HOST", "localhost"); // Your database host name define ("DB_USER", "root"); // Your database user define ("DB_PASS",""); // Your database password define ("DB_NAME",""); // Your database name $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection."); $db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database"); ?> |
function.php // set Database table name here
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | <?php function displayPaginationBelow($per_page,$page){ $page_url="?"; $query = "SELECT COUNT(*) as totalCount FROM DATABSE_TABLE_NAME where status = 1"; $rec = mysql_fetch_array(mysql_query($query)); $total = $rec['totalCount']; $adjacents = "2"; $page = ($page == 0 ? 1 : $page); $start = ($page - 1) * $per_page; $prev = $page - 1; $next = $page + 1; $setLastpage = ceil($total/$per_page); $lpm1 = $setLastpage - 1; $setPaginate = ""; if($setLastpage > 1) { $setPaginate .= "<ul class='setPaginate'>"; $setPaginate .= "<li class='setPage'>Page $page of $setLastpage</li>"; if ($setLastpage < 7 + ($adjacents * 2)) { for ($counter = 1; $counter <= $setLastpage; $counter++) { if ($counter == $page) $setPaginate.= "<li><a class='current_page'>$counter</a></li>"; else $setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>"; } } elseif($setLastpage > 5 + ($adjacents * 2)) { if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $setPaginate.= "<li><a class='current_page'>$counter</a></li>"; else $setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>"; } $setPaginate.= "<li class='dot'>...</li>"; $setPaginate.= "<li><a href='{$page_url}page=$lpm1'>$lpm1</a></li>"; $setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>$setLastpage</a></li>"; } elseif($setLastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $setPaginate.= "<li><a href='{$page_url}page=1'>1</a></li>"; $setPaginate.= "<li><a href='{$page_url}page=2'>2</a></li>"; $setPaginate.= "<li class='dot'>...</li>"; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $setPaginate.= "<li><a class='current_page'>$counter</a></li>"; else $setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>"; } $setPaginate.= "<li class='dot'>..</li>"; $setPaginate.= "<li><a href='{$page_url}page=$lpm1'>$lpm1</a></li>"; $setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>$setLastpage</a></li>"; } else { $setPaginate.= "<li><a href='{$page_url}page=1'>1</a></li>"; $setPaginate.= "<li><a href='{$page_url}page=2'>2</a></li>"; $setPaginate.= "<li class='dot'>..</li>"; for ($counter = $setLastpage - (2 + ($adjacents * 2)); $counter <= $setLastpage; $counter++) { if ($counter == $page) $setPaginate.= "<li><a class='current_page'>$counter</a></li>"; else $setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>"; } } } if ($page < $counter - 1){ $setPaginate.= "<li><a href='{$page_url}page=$next'>Next</a></li>"; $setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>Last</a></li>"; }else{ $setPaginate.= "<li><a class='current_page'>Next</a></li>"; $setPaginate.= "<li><a class='current_page'>Last</a></li>"; } $setPaginate.= "</ul>\n"; } return $setPaginate; } ?> |
index.php // View pagination with CSS & Function call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | <?php //Make Database connectivity include_once "dbConfig.php"; include_once "function.php"; if(isset($_GET["page"])) $page = (int)$_GET["page"]; else $page = 1; $setLimit = 10; $pageLimit = ($page * $setLimit) - $setLimit; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Download Pagination in PHP and MySql With Example</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style type="text/css"> .navi { width: 500px; margin: 5px; padding:2px 5px; border:1px solid #eee; } .show { color: blue; margin: 5px 0; padding: 3px 5px; cursor: pointer; font: 15px/19px Arial,Helvetica,sans-serif; } .show a { text-decoration: none; } .show:hover { text-decoration: underline; } ul.setPaginate li.setPage{ padding:15px 10px; font-size:14px; } ul.setPaginate{ margin:0px; padding:0px; height:100%; overflow:hidden; font:12px 'Tahoma'; list-style-type:none; } ul.setPaginate li.dot{padding: 3px 0;} ul.setPaginate li{ float:left; margin:0px; padding:0px; margin-left:5px; } ul.setPaginate li a { background: none repeat scroll 0 0 #ffffff; border: 1px solid #cccccc; color: #999999; display: inline-block; font: 15px/25px Arial,Helvetica,sans-serif; margin: 5px 3px 0 0; padding: 0 5px; text-align: center; text-decoration: none; } ul.setPaginate li a:hover, ul.setPaginate li a.current_page { background: none repeat scroll 0 0 #0d92e1; border: 1px solid #000000; color: #ffffff; text-decoration: none; } ul.setPaginate li a{ color:black; display:block; text-decoration:none; padding:5px 8px; text-decoration: none; } </style> </head> <body> <div class="navi"> <?php // Your SQL query go here. This query will display all record by setting the Limit. $sql = "SELECT * FROM TABLE_NAME where status = 1 LIMIT ".$pageLimit." , ".$setLimit; $query = mysql_query($sql); while ($rec = mysql_fetch_array($query)) { ?> <div class="show"><a href="http://www.discussdesk.com/<?php echo $rec["url"];?>.htm" target="_blank"><?php echo $rec['title'];?></a></div> <?php } ?> </div> <?php // Call the Pagination Function to load Pagination. echo displayPaginationBelow($setLimit,$page); ?> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-38304687-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </body> </html> <!--******************************************** For More Detail please Visit: http://www.discussdesk.com/download-pagination-in-php-and-mysql-with-example.htm ************************************************--> |
Source: http://www.discussdesk.com/download-pagination-in-php-and-mysql-with-example.htm