ik wil slechts 15 regels van een opgeroepen database tonen en dan onderaan de mogelijkheid door te klikken naar de volgende 15.
hoe doe je dat?
3.225 views
<?php
// assumptions
// - you have a local database running under database, user and password "test"
// - you have a config table you read entries from:
/*
CREATE TABLE `config` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`_key` varchar(255) NOT NULL,
`_value` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `_key` (`_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
*/
// debugging
error_reporting(E_ALL);
ini_set('display_errors', 'stdout');
// helper functions
function escape($in) {
return htmlspecialchars($in, ENT_QUOTES, 'UTF-8');
}
function isIndex($in) {
// note: don't forget to trim or typecast (note that this is warranted when function returns true)
return preg_match('#^[1-9][0-9]*$#', $in) === 1;
}
// connect
$db = new mysqli('127.0.0.1', 'test', 'test', 'test');
$db->set_charset('utf8');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>paging test</title>
<style type="text/css">
div.pagination { }
div.pagination > div { float: left; display: block; width: 50px; height: 50px; text-align: center; line-height: 50px; background-color: #ffcccc; margin: 5px; }
div.pagination > div.active { background-color: #ff6666; }
div.pagination > div a { display: block; width: 100%; height: 100%; text-decoration: none; color: #000000; }
div.pagination > div a:hover { background-color: #ffaaaa; }
div.clear { clear: both; }
</style>
</head>
<body><?php
// determine page - starting from page zero
$currentPage = isset($_GET['p']) && isIndex($_GET['p']) ? (int) $_GET['p'] : 0;
// limit items per page
$limit = 5;
// determine item offset
$offset = $currentPage * $limit;
// get items of current page, notice the SQL_CALC_FOUND_ROWS flag
$results = $db->query(
'SELECT SQL_CALC_FOUND_ROWS id, _key, _value
FROM config
ORDER BY id
LIMIT '.$offset.', '.$limit
);
// determine total # items and calculate # pages
$tmp = $db->query('SELECT FOUND_ROWS()')->fetch_row();
$totalItems = $tmp[0];
$totalPages = ceil($totalItems / $limit);
// print navigation - only when there is something to print
if ($totalPages > 1) {
?><div class="pagination"><?php
// previous page?
if ($currentPage > 0) {
?><div><a href="?p=<?php echo escape($currentPage - 1) ?>"><</a></div><?php
} else {
?><div> </div><?php
}
for ($p = 0; $p < $totalPages; $p++) {
$currentActive = $p == $currentPage;
?><div<?php echo ($currentActive ? ' class="active"' : '') ?>><?php
if ($currentActive) {
// display "natural" page numbers, starting from 1
?><strong><?php echo escape($p + 1) ?></strong><?php
} else {
?><a href="?p=<?php echo escape($p) ?>"><?php echo escape($p + 1) ?></a><?php
}
?></div><?php
}
// next page?
if ($currentPage + 1 < $totalPages) {
?><div><a href="?p=<?php echo escape($currentPage + 1) ?>">></a></div><?php
} else {
?><div> </div><?php
}
?></div>
<div class="clear"><!-- clear --></div><?php
}
// print items
if ($results->num_rows) {
?><ul><?php
while ($row = $results->fetch_assoc()) {
?><li><strong><?php echo escape($row['_key']) ?></strong> - <?php echo escape($row['_value']) ?></li><?php
}
?></ul><?php
} else {
?><p>No results (for this page)</p><?php
}
$results->free();
?></body>
</html>
Thomas van den Heuvel op 14/08/2015 13:28:39MySQL heeft ook een handige voorziening waarbij je de LIMIT-query kunt voorzien van een extra [color=#00cc00]"flag"[/color] die ervoor zorgt dat je met een tweede vereenvoudigde query snel kunt opvragen hoeveel resultaten er in totaal zouden zijn zonder de limitering.