简体   繁体   中英

display 5 result per page

I'm new to this, and was hoping someone may be able to help me, I have a mysql database with 4 fields "id, date, name, comment" I have the following php file:

<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);
?>

This all works fine, apart from I want it do display 5 results per page, can someone please tell me what I need to change, how and where? Many thanks

This is working code. Just copy and paste. Do your echo. It will look nice if you echo it in table.

<?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);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM