0

我是新手,希望有人能帮助我,我有一个 mysql 数据库,其中包含 4 个字段“id、日期、名称、评论”我有以下 php 文件:

<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$rec_limit = 5;

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('DATABASE_NAME');
/* Get total number of records */
$sql = "SELECT count(id) FROM comments ";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];

if( isset($_GET{'page'} ) )
{
   $page = $_GET{'page'} + 1;
   $offset = $rec_limit * $page ;
}
else
{
   $page = 0;
   $offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);

$sql = "SELECT * FROM comments ORDER BY id DESC"; 
       "FROM comments ".
       "LIMIT $offset, $rec_limit";


$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "<strong>Date :{$row['date']}  </strong><br> ".
         "<strong>Name : {$row['name']} </strong><br> ".
         "Comment : {$row['comment']} <br> ".
         "<br><br>";
} 

if( $page > 0 )
{
   $last = $page - 2;
   echo "<a href=\"$_PHP_SELF?page=$last\">Previous Page</a> |";
   echo "<a href=\"$_PHP_SELF?page=$page\">Next Page</a>";
}
else if( $page == 0 )
{
   echo "<a href=\"$_PHP_SELF?page=$page\">Next Page</a>";
}
else if( $left_rec < $rec_limit )
{
   $last = $page - 2;
   echo "<a href=\"$_PHP_SELF?page=$last\">Previous Page</a>";
}
mysql_close($conn);
?>

这一切都很好,除了我希望它每页显示 5 个结果之外,有人可以告诉我我需要更改什么,如何更改以及在哪里更改?非常感谢

4

1 回答 1

2

这是工作代码。只需复制和粘贴。做你的回声。如果你在表中回显它会很好看。

<?php

//DB CONNECTION HERE

$currentPage = $_SERVER["PHP_SELF"];

$rec_limit = 5;//This will display 5 result per page
$pageNum_view = 0;
if (isset($_GET['pageNum_view'])) {
   $pageNum_view = $_GET['pageNum_view'];
}
$startRow_view = $pageNum_view * $rec_limit;

mysql_select_db($dbhost, $dbuser, dbpass);
$query_view = "SELECT * FROM comments ORDER BY id DESC";
$query_limit_view = sprintf("%s LIMIT %d, %d", $query_view, $startRow_view, $rec_limit);
$view = mysql_query($query_limit_view, $dbconnect) or die(mysql_error());
$row_view = mysql_fetch_assoc($view);

if (isset($_GET['totalRows_view'])) {
$totalRows_view = $_GET['totalRows_view'];
} else {
  $all_view = mysql_query($query_view);
     $totalRows_view = mysql_num_rows($all_view);
}
     $totalPages_view = ceil($totalRows_view/$rec_limit)-1;

$queryString_view = "";
if (!empty($_SERVER['QUERY_STRING'])) {
   $params = explode("&", $_SERVER['QUERY_STRING']);
    $newParams = array();
  foreach ($params as $param) {
  if (stristr($param, "pageNum_view") == false && 
    stristr($param, "totalRows_view") == false) {
  array_push($newParams, $param);
  }
 }
  if (count($newParams) != 0) {
  $queryString_view = "&" . htmlentities(implode("&", $newParams));
 }
 }
$queryString_view = sprintf("&totalRows_view=%d%s", $totalRows_view, 
$queryString_view);
?>
//Here echo
<?php if ($pageNum_view > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_view=%d%s", $currentPage, 0, 
$queryString_view); ?>">FIRST</a>
      <?php } // Show if not first page ?>

<?php if ($pageNum_view > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_view=%d%s", $currentPage, max(0, $pageNum_view - 1), $queryString_view); ?>">PREVIOUS</a>
      <?php } // Show if not first page ?>

 <?php if ($pageNum_view < $totalPages_view) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_view=%d%s", $currentPage, min($totalPages_view, $pageNum_view + 1), $queryString_view); ?>">NEXT</a>
      <?php } // Show if not last page ?>

   <?php if ($pageNum_view < $totalPages_view) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_view=%d%s", $currentPage, $totalPages_view, $queryString_view); ?>">LAST</a>
      <?php } // Show if not last page ?>

<?php
   mysql_free_result($view);
?>
于 2013-07-26T09:08:32.390 回答