我正在关注基本php表的教程,向用户展示分页:
我遇到的问题是我的平台不支持MySQL所以我将其更改为MySQLi
第二个问题是它应该每页只显示一个用户,但它只显示1页中的所有用户,当点击例如第2页时,它显示错误页面
这是一张更好描述的图片:
http://store2.up-00.com/2014-02/1391920754996.jpg
这是我正在使用的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'players' table
This is a modified version of view.php that includes pagination
*/
// connect to the database
include('config.php');
// number of results to show per page
$per_page = 1;
// figure out the total pages in the database
$result = mysqli_query($connecDB,"SELECT * FROM users");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
}
echo "</p>";
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$fname = $row['first_name'];
$lname = $row['last_name'];
echo "<tr><td>";
echo $id;
echo "</td>";
echo "<td>";
echo $fname;
echo "<td>";
echo $lname;
echo "</td></tr>";
}
echo "</td></tr>";
}
// close table>
echo "</table>";
// pagination
?>
<p><a href="admin/admin_user_add.php">Add a new record</a></p>
</body>
</html>
答案 0 :(得分:1)
我无法修复您现有的脚本,因此我找到了一个我修改过的分页脚本,并为我工作过。
你无疑会想要修改它,但它确实有用。
只需更改数据库凭据以及整个脚本中可以找到的其他内容。
在评论选项中也有一些评论。
<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";
$db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db->connect_errno > 0) {
die('Connection failed [' . $db->connect_error . ']');
}
$sql2 = "SELECT COUNT(id) FROM users ";
$query2 = mysqli_query($db, $sql2);
$row = mysqli_fetch_row($query2);
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 1;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1; // do not change this
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$sql = "SELECT id, first_name, last_name FROM users ORDER BY id ASC $limit";
$query = mysqli_query($db, $sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Names (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
}
}
$dynamicList = '';
// display data in table
echo "<table border='1' cellpadding='10'>";
// echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";
echo "<tr>";
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
/*
$id = $row["id"];
$product_name = $row["first_name"];
$price = $row["last_name"];
*/
$id = $row['id'];
$fname = $row['first_name'];
$lname = $row['last_name'];
echo "<tr><td>";
echo $id;
echo "</td>";
echo "<td>";
echo $fname;
echo "<td>";
echo $lname;
echo "</td></tr>";
// you can use and modify this below
// the <!-- and --> tags can be taken out. Those are regular HTML comment tags.
$dynamicList .= "
<!--
<li><div class='product'>
<a href='pager.php?id=$id' class='info'>
<span class='holder'>
<img src='images/$id.jpg' alt='$product_name' />
<span class='book-name'>$product_name</span>
</a>
<a href='pager.php?id=$id' class='buy-btn'> (link) <span class='price'>$price</span></a>
</div>
</li>
-->
";
}
// Close your database connection
mysqli_close($db);
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
div#pagination_controls{font-size:21px;}
div#pagination_controls > a{ color:#06F; }
div#pagination_controls > a:visited{ color:#06F; }
</style>
</head>
<body>
<div>
<h2><?php echo $textline1; ?> Paged</h2>
<p><?php echo $textline2; ?></p>
<p><?php echo $dynamicList; ?></p>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>