Jump to content

Pagination help


mcurtis

Recommended Posts

Hi phpfreaks,

 

I'm trying to get a '10 result per page' pagination but it just doesn't seem to want to work properly... It does actually put the [page number] at the bottom of the page, but displays ALL the results on 1 long page instead of splitting them up in to different pages of 10.

 

If anyone could help, I would seriously appreciate it!!!

 

<!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" xml:lang="en-gb" lang="en-gb" dir="ltr" >

<head>
<title>Renewal Dates</title>
<meta name="robots" content="index,no_follow">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>

<?

// Connects to your Database
include("db_connect.php");

// Connect to server and select database.
$conn=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db($db_name,$conn) or die ("cannot select DB");

$query="SELECT * FROM users";
$result=mysql_query($query);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
    $rowsperpage = 10;
// find out total pages
    $totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
    $currentpage = (int) $_GET['currentpage'];
    } else {
// default page num
    $currentpage = 1;
    } // end if
// if current page is greater than total pages...
    if ($currentpage > $totalpages) {
// set current page to last page
    $currentpage = $totalpages;
    } // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
   $currentpage = 1;
    } // end if
// the offset of the list, based on current page 
    $offset = ($currentpage - 1) * $rowsperpage;
    $num=mysql_numrows($result);
    mysql_close();
    $i=0;
    while ($i < $num) {

$field_1=mysql_result($result,$i,"field_1");
$field_2=mysql_result($result,$i,"field_2");
$field_3=mysql_result($result,$i,"field_3");
$field_4=mysql_result($result,$i,"field_4");
$field_5=mysql_result($result,$i,"field_5");
$field_6=mysql_result($result,$i,"field_6");
$field_7=mysql_result($result,$i,"field_7");
$field_8=mysql_result($result,$i,"field_8");

// while there are rows to be fetched...
   // echo data
echo "<table width=100% align=center>";
echo "<tbody>";
echo "<tr>";
echo "<td colspan=4><hr width=100% size=2 /></td>";
        echo "</tr>";
echo "<tr>";
        echo "<td align=left><b>ID: $field_1</b></td>";
        echo "<td align=left><b>$field_2</b></td>";
echo "</tr>";
echo "<tr>";	
echo "<td align=left colspan=2>field 3: <b>$field_3</b></td>";
echo "<td align=right colspan=2>field 4: <b>€$field_4</b></td>";
        echo "</tr>";
echo "<tr>";
        echo "<td align=left>field 5: <b>$field_5</b></td>";
        echo "<td align=left>field 6: <b>$field_6</b></td>";
echo "<td align=left colspan=2>field 7: <b>$field_7</b></td>";
        echo "</tr>";
echo "<tr>";
        echo "<td align=left colspan=4>field 8: $field_8</td>";
        echo "</tr>";
echo "</tbody>";
echo "</table>";
    $i++;
    }

?></td>

<?
/******  build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>start</a> | ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>prev</a> | ";
} // end if 
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
// echo forward link for next page 
   echo " | <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>next</a> ";
// echo forward link for lastpage
   echo " | <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>end</a> ";
} // end if

/****** end build pagination links ******/
?>
</body>

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

Thanks for your answer btherl - but do I not already use $offset, $rowsperpage and $i in:

// the offset of the list, based on current page 
    $offset = ($currentpage - 1) * $rowsperpage;
    $num=mysql_numrows($result);
    mysql_close();
    $i=0;
    while ($i < $num) {

Link to comment
https://forums.phpfreaks.com/topic/246235-pagination-help/#findComment-1264592
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.