PHP en MySQL

Bootstrap Pagination

PHP en MySQL Pagination met Bootstrap
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP and MySQL | Bootstrap Pagination</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<style>
/* START DEMO PAGE STYLES */
.return-to-website-header {
margin-bottom: 2em;
padding: 1em;
}
.return-to-website-header a {
margin-bottom: 2em;
text-decoration: none;
}
.return-to-website-header a:hover {
margin-bottom: 2em;
color: #ff9900;
}
/* END DEMO PAGE STYLES */
.card-text {
margin-bottom: 2em;
}
.pagination a {
margin: 0 4px;
}
.card-header {
padding: .5em 1em;
}
</style>
</head>
<body>
<div class="return-to-website-header"><a href="../"><i class="fas fa-chevron-circle-left"></i> click here to return to the website</a>. This is demonstration page.</div>
<div class="container">
<div class="card-columns">
<?php
$server_name = "xxx.xxx.xxx.xxx";
$user_name = "**********";
$user_password = "**********";
$database_name = "xyxyxyxyxyY";
$table_name = "top_5000_songs";
$error_message_no_connection = "Connection Error";
$error_message_no_results = "No records available";
$conn = new mysqli ($server_name, $user_name, $user_password, $database_name);
mysqli_set_charset ($conn, "utf8");
if ($conn->connect_error) {
die ("<strong>" . $error_message_no_connection . "</strong>:</br>" . $conn->connect_error);
}
?>
<?php
$adjacents = 2;
$query_c = "SELECT * FROM ".$table_name." WHERE `year` = '1986' "; 
$result_c = $conn->query($query_c);
$row_count = $result_c->num_rows; 
$total_pages = $row_count;
$startPage = ""; // index.php
$limit = 5;
$page = $_GET['page'];
if($page) 
$start = ($page - 1) * $limit;
else
$start = 0;
$query_s = "SELECT artist, SUBSTRING_INDEX(song,' ',6) AS song, year FROM ".$table_name." WHERE `year` = '1986' LIMIT $start, $limit";
$result_s = $conn->query($query_s);
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1){
$pagination .= "<ul class='pagination pagination-sm justify-content-center flex-sm-wrap'>";
if ($page > 1) 
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$prev'>«</a></li>";
else
$pagination.= "<li class='page-item disable'><a class='page-link' href='#'>«</a></li>"; 
if ($lastpage < 7 + ($adjacents * 2)){ 
for ($counter = 1; $counter <= $lastpage; $counter++){
if ($counter == $page)
$pagination.= "<li class='page-item active'><a class='page-link' href='#'>$counter</a></li>";
else
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$counter'>$counter</a></li>"; 
}
}
elseif ($lastpage > 5 + ($adjacents * 2)) 
{
if ($page < 1 + ($adjacents * 2)) 
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<li class='page-item active'><a class='page-link' href='#'>$counter<a></li>";
else
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$counter'>$counter</a></li>"; 
}
$pagination.= "<li class='page-item'>...</li>";
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$lastpage'>$lastpage</a></li>"; 
}
elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=1'>1</a></li>";
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=2'>2</a></li>";
$pagination.= "<li class='page-item'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<li class='page-item active'><a class='page-link' href='#'>$counter</a></li>";
else
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$counter'>$counter</a></li>"; 
}
$pagination.= "<li class='page-item'>...</li>";
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$lastpage'>$lastpage</a></li>"; 
}
else
{
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=1'>1</a></li>";
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=2'>2</a></li>";
$pagination.= "<li class='page-item'>...</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<li class='page-item active'><a class='page-link' href='#'>$counter</a></li>";
else
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$counter'>$counter</a></li>"; 
}
}
}
if ($page < $counter - 1) 
$pagination.= "<li class='page-item'><a class='page-link' href='$startPage?page=$next'>»</a></li>";
else
$pagination.= "<li class='page-item disabled'><a class='page-link' href='#'>»</a></li>";
$pagination.= "</ul>"; 
}
?>
<?php
if ($result_s->num_rows > 0) {
while ($row = $result_s->fetch_assoc()) { 
echo '<div class="card text-white bg-secondary">' . "\n";
echo '<div class="card-header">' . $row["artist"] . '</div>' . "\n";
echo '<div class="card-body">' . "\n";
echo '<div class="card-text">'. $row["song"] . ' (' . $row["year"] . ')</div>' . "\n";
echo '</div>' . "\n";
echo '</div>' . "\n";
}
} 
else {
echo '<strong>' . $error_message_no_results . '</strong>';
}
$conn->close();
?>
</div>
</div>
<div style="margin-top: 2em;">
<?=$pagination?>
</div>
</div>
</body>
</html>

-