php - 每页用户显示的分页问题

标签 php mysql mysqli pagination

我正在学习关于基本 php 表格的教程,以向用户展示分页:

http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/

我遇到的问题是我的平台不支持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>

最佳答案

我无法修复您现有的脚本,所以我找到了一个我修改过的分页脚本,并且对我有用。

毫无疑问,您会想要修改它,但它确实有效。

只需更改数据库凭据和您将在整个脚本中找到的其他内容。

其中也有一些关于注释掉的选项的评论。

<?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> &nbsp; &nbsp; ';
        // 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> &nbsp; ';
            }
        }
    }
    // Render the target page number, but without it being a link
    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    // 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> &nbsp; ';
        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 .= ' &nbsp; &nbsp; <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>

关于php - 每页用户显示的分页问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21655520/

相关文章:

PHP Mysql 试图从不同的表中提取多行(适用于 2 个表中的 3 行,但不适用于 3 个表中的 4 行)

javascript - 在数据库中将设置保存为 JSON 与将它们保存在额外的行中

php - 从第三个表中获取值(value)

php - 子查询计算错误?

php - laravel 从 3 个表中选择所有行

javascript - 当字符串与 header PHP 不匹配时将列留空

php - Wordpress - 如何在 woocommerce 中使运费为零?

php - 如何对 MYSQL 进行多次插入,每次插入都基于子查询中一组值中的每个值?

mysql - 为什么这个正则表达式在 mysql 中不起作用?

php - mysqli:它可以在一条语句中准备多个查询吗?