Jump to content

Simple PHP Pagination Help


NerdArmy

Recommended Posts

I'm teaching my self programing and gradually getting better. However I'm running into some slight problems. I'm working on a website and I currently have pagination at the bottom of some mysql DB results. My pagination works, buts it's Dreamweavers default pagination which is simply (first, next, previous, last). I would like to upgrade it so it would display actual numbers for example 1,2,3 etc. I've found a php script that I would like to use. My problem is I don't fully understand how to implement it within my code. I'm a php newbie and I rely heavily on dreamweaver. It would be greatly appreciated if someone could help me through this.

 

Here's the dreamweaver default pagination:

<?php require_once('Connections/myconnectboi.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Recordset2 = 4;
$pageNum_Recordset2 = 0;
if (isset($_GET['pageNum_Recordset2'])) {
  $pageNum_Recordset2 = $_GET['pageNum_Recordset2'];
}
$startRow_Recordset2 = $pageNum_Recordset2 * $maxRows_Recordset2;

mysql_select_db($database_myconnectboi, $myconnectboi);
$query_Recordset2 = "SELECT * FROM hiphop ORDER BY id DESC";
$query_limit_Recordset2 = sprintf("%s LIMIT %d, %d", $query_Recordset2, $startRow_Recordset2, $maxRows_Recordset2);
$Recordset2 = mysql_query($query_limit_Recordset2, $myconnectboi) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);

if (isset($_GET['totalRows_Recordset2'])) {
  $totalRows_Recordset2 = $_GET['totalRows_Recordset2'];
} else {
  $all_Recordset2 = mysql_query($query_Recordset2);
  $totalRows_Recordset2 = mysql_num_rows($all_Recordset2);
}
$totalPages_Recordset2 = ceil($totalRows_Recordset2/$maxRows_Recordset2)-1;

$queryString_Recordset2 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset2") == false &&
        stristr($param, "totalRows_Recordset2") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset2 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset2 = sprintf("&totalRows_Recordset2=%d%s", $totalRows_Recordset2, $queryString_Recordset2);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table cellspacing="5">
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <?php do { ?>
    <tr>
  
      <td><img src="<?php echo $row_Recordset2['image']; ?>" width="300" height="250" />;</td>
      <td width="300" height="250" align="center"><?php echo $row_Recordset2['description']; ?></td>
     
    </tr>
    <?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
</table><table border="0">
  <tr>
    <td><?php if ($pageNum_Recordset2 > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_Recordset2=%d%s", $currentPage, 0, $queryString_Recordset2); ?>">First</a>
        <?php } // Show if not first page ?></td>
    <td><?php if ($pageNum_Recordset2 > 0) { // Show if not first page ?>
        <a href="<?php printf("%s?pageNum_Recordset2=%d%s", $currentPage, max(0, $pageNum_Recordset2 - 1), $queryString_Recordset2); ?>">Previous</a>
        <?php } // Show if not first page ?></td>
    <td><?php if ($pageNum_Recordset2 < $totalPages_Recordset2) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_Recordset2=%d%s", $currentPage, min($totalPages_Recordset2, $pageNum_Recordset2 + 1), $queryString_Recordset2); ?>">Next</a>
        <?php } // Show if not last page ?></td>
    <td><?php if ($pageNum_Recordset2 < $totalPages_Recordset2) { // Show if not last page ?>
        <a href="<?php printf("%s?pageNum_Recordset2=%d%s", $currentPage, $totalPages_Recordset2, $queryString_Recordset2); ?>">Last</a>
        <?php } // Show if not last page ?></td>
  </tr>
</table>
</p>
</body>
</html>
<?php
mysql_free_result($Recordset2);
?>

Here's the pagination I would like to use. I got it form http://www.phpeasycode.com/pagination/

 

<?php
/*************************************************************************
php easy :: pagination scripts set - Version Three
==========================================================================
Author:      php easy code, www.phpeasycode.com
Web Site:    http://www.phpeasycode.com
Contact:     [email protected]
*************************************************************************/
function paginate_three($reload, $page, $tpages, $adjacents) {
   
    $prevlabel = "‹ Prev";
    $nextlabel = "Next ›";
   
    $out = "<div class=\"pagin\">\n";
   
    // previous
    if($page==1) {
        $out.= "<span>" . $prevlabel . "</span>\n";
    }
    elseif($page==2) {
        $out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
    }
    else {
        $out.= "<a href=\"" . $reload . "&page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
    }
   
    // first
    if($page>($adjacents+1)) {
        $out.= "<a href=\"" . $reload . "\">1</a>\n";
    }
   
    // interval
    if($page>($adjacents+2)) {
        $out.= "...\n";
    }
   
    // pages
    $pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
    $pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
    for($i=$pmin; $i<=$pmax; $i++) {
        if($i==$page) {
            $out.= "<span class=\"current\">" . $i . "</span>\n";
        }
        elseif($i==1) {
            $out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
        }
        else {
            $out.= "<a href=\"" . $reload . "&page=" . $i . "\">" . $i . "</a>\n";
        }
    }
   
    // interval
    if($page<($tpages-$adjacents-1)) {
        $out.= "...\n";
    }
   
    // last
    if($page<($tpages-$adjacents)) {
        $out.= "<a href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>\n";
    }
   
    // next
    if($page<$tpages) {
        $out.= "<a href=\"" . $reload . "&page=" . ($page+1) . "\">" . $nextlabel . "</a>\n";
    }
    else {
        $out.= "<span>" . $nextlabel . "</span>\n";
    }
   
    $out.= "</div>";
   
    return $out;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/248372-simple-php-pagination-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.