Backend Development
Pagination
Submitted by linkzy, 14-07-2016, 08:13 PM, Thread ID: 22382
Thread Closed
RE: Pagination
14-07-2016, 08:13 PMlinkzy Wrote: How do i paginate this thing.
-
Why so many name changes Believer ;3
Code:
<html>
<body style="background: #d2d2d2;">
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<center><h1>User List</h1></center>
<table>
<tr>
<th>Name</th>
<th>Cash</th>
<th>Score</th>
<?php
$conn = mysql_connect('localhost','root','');
$db = mysql_select_db('users', $conn) or die(mysql_error());
function pagination(){
global $conn;
$sql = "SELECT COUNT(*) FROM users";
$count = mysql_query($sql, $conn);
$result = mysql_fetch_row($count);
$totalrows = $result[0];
// number of rows to show per page
$itemsperpage = 1;
// find out total pages
$totalpages = ceil($totalrows / $itemsperpage);
// get the current page or set a default
if (isset($_GET['p']) && is_numeric($_GET['p']) && is_numeric($_GET['p'])) {
// cast var as int
$p = $_GET['p'];
} else {
// default page num
$p = 1;
} // end if
// if current page is greater than total pages...
if ($p > $totalpages) {
// set current page to last page
$p = $totalpages;
} // end if
// if current page is less than first page...
if ($p < 1) {
// set current page to first page
$p = 1;
} // end if
// the offset of the list, based on current page
$offset = ($p - 1) * $itemsperpage;
$range = 3;
// if not on page 1, don't show back links
if ($p > 1) {
// show << link to go back to page 1
echo "<li><a href='{$_SERVER['PHP_SELF']}?p=1'><<</a></li>";
// get previous page num
$prevpage = $p - 1;
// show < link to go back to 1 page
echo "<li><a href='{$_SERVER['PHP_SELF']}?p=$prevpage'><</a></li>";
} // end if
// loop to show links to range of pages around current page
for ($x = ($p - $range); $x < (($p + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $p) {
// 'highlight' it but don't make a link
echo "<li class='active'><a href='#'>$x</a></li>";
// if not current page...
} else {
// make it a link
echo "<li><a href='{$_SERVER['PHP_SELF']}?p=$x'>$x</a></li>";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($p != $totalpages) {
// get next page
$nextpage = $p + 1;
// echo forward link for next page
echo "<li><a href='{$_SERVER['PHP_SELF']}?p=$nextpage'>></a></li>";
// echo forward link for lastpage
echo "<li><a href='{$_SERVER['PHP_SELF']}?p=$totalpages'>>></a></li>";
} // end if
/****** end build pagination links ******/
$query = mysql_query("select username, avatar from mybb_users LIMIT $offset, $itemsperpage", $conn);
while($row = mysql_fetch_array($query))
{
?>
<tr>
<td class="left"><?php echo $row['Name']; ?></td>
<td class="left"><?php echo $row['Cash']; ?></td>
<td class="left"><?php echo $row['Score']; ?></td>
</tr>
<?php
}
}
?>
<ul class="pagination">
<?= pagination() ?>
</ul>
</table>
</body>
</html>
Source: http://www.phpfreaks.com/tutorial/basic-pagination
Use MySQLI please.
Users browsing this thread: 3 Guest(s)