スクリプトが機能するかどうかわかりません。問題は、レコードを削除または追加するか、$query .= "WHERE visible = 1 を変更する場合です。たとえば、5 つのレコードを削除すると、次のように表示されます。
ページネーターは削除されたレコードを表示しません。リフレッシュしてもFirefoxhttp://localhost/dwwithphp/public/admin/list_photos_5.php?s=40&p=11&sort=ln
をリフレッシュしても何も起こらずhttp://localhost/dwwithphp/public/admin/list_photos_5.php
、変更が表示されます
リフレッシュ後/admin/list_photos_5.php
ページネーションはそのように機能しますか、それとも何か間違っていますか?
<?php
//1. database connect
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "3edcvfr4";
$dbname = "dw_bookstore";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//change utf8
if (!mysqli_set_charset($connection, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($connection));
} else {
//printf("set na: %s\n", mysqli_character_set_name($connection));
}
?>
<?php
// Number of records to show per page:
$display = 5;
// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.
$pages = $_GET['p'];
} else { // Need to determine.
// Determine how many pages there are...
$query = "SELECT COUNT(id) FROM photographs";
$result = mysqli_query ($connection, $query);
$row = mysqli_fetch_array ($result, MYSQLI_NUM);
$records = $row[0];
// Calculate the number of pages...
if ($records > $display) { // More than 1 page.
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
} // End of p IF.
// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
// Determine the sort...
// Default is by id.
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'rd';
// Determine the sorting order:
switch ($sort) {
case 'ln':
$order_by = 'filename ASC';
break;
case 'fn':
$order_by = 'size ASC';
break;
case 'rd':
$order_by = 'nazwa ASC';
break;
default:
$order_by = 'id ASC';
$sort = 'rd';
break;
}
// Make the query:
$query = "SELECT id, filename, size, type, nazwa, kod ";
$query .= "FROM photographs ";
$query .= "WHERE visible = 1 ";
$query .= "ORDER BY $order_by LIMIT $start, $display ";
$result = mysqli_query ($connection, $query);
?>
<?php
// 2. Perform database query
$query2 = "SELECT * ";
$query2 .= "FROM photographs ";
$query2 .= "WHERE visible = 1 ";
$query2 .= "ORDER BY nazwa ASC ";
$result2 = mysqli_query($connection, $query2);
// Test if there was a query error
if (!$result2) {
die("Database query failed.");
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>List_n01</title>
<link href="../../stylesheets/main_2.css" rel="stylesheet" type="text/css" media="screen, projection" />
</head>
<body>
<div id="wrapper">
<div id="header">
<h2>Cennik: Panel Administracyjny</h2>
</div>
<div id="mainContent">
<h1>Graphic Design</h1>
<?php
// Count the number of returned rows:
$num = mysqli_num_rows($result2);
if ($num > 0) { // If it ran OK, display the records.
// Print how many rows there are:
echo "<p>W bazie znajduje się $num pozycji.</p>\n"; ?>
<table id="article">
<caption></caption>
<colgroup>
</colgroup>
<tr>
<th><a href="list_photos_5.php?sort=ln">id</a></th>
<th>filename</th>
<th>size</th>
<th>type<a href="list_photos_5.php?sort=fn">Nazwa:</a></th>
<th>name</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr>
<td align="left">' . $row['id'] . '</td>
<td align="left">' . $row['filename'] . '</td>
<td align="left">' . $row['size'] . '</td>
<td align="left">' . $row['type'] . '</td>
<td align="left">' . $row['nazwa'] . '</td>
</tr>
';
} // End of WHILE loop.
echo '</table>';
?>
<?php
// 4. Release returned data
mysqli_free_result($result);
} else { // If no records were returned.
echo '<p class="error">There are currently no rows.</p>';
}
?>
<?php
// Make the links to other pages, if necessary.
if ($pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button:
if ($current_page != 1) {
echo '<a href="list_photos_5.php?s=' . ($start - $display) . '&p=' . $pages . '&sort=' . $sort . '">Previous</a> ';
}
// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
$distance = $current_page - $i;
if (abs($distance) < 5){
echo '<a href="list_photos_5.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&sort=' . $sort . '">' . $i . '</a> ';
}
} else {
echo $i . ' ';
}
} // End of FOR loop
// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo '<a href="list_photos_5.php?s=' . ($start + $display) . '&p=' . $pages . '&sort=' . $sort . '">Next</a>';
}
echo '</p>'; // Close the paragraph.
} // End of links section.
?>
</div>
<div id="footer">
<p>Copyright <?php echo date("Y", time()); ?>, Unixlab</p></div>
</div>
</body>
</html>
<?php
// 5. Close database connection
mysqli_close($connection);
?>