Jump to content

PHPFreaks Tutorial - Pagination help


c-o-d-e

Recommended Posts

The pagination from PHPFreaks tutorial, should be displayed as

<< < 1 [2] 3 4 > >>

etc. This worked fine if I had 4 pages of results.

I deleted my test results, and I am down to 2 results.

Though it still goes up to 4. When it should display one.

 

The changes I made in the code, are the queries, and setting it so that << and < etc are displayed at all times which I put them as they are, with an else statement afterwards.

 

You can see what I mean here.

www.developers-community.com/allnews.php

 

Here is the PHP.

include 'config.php';
$result = mysql_query("SELECT * FROM News ORDER BY Posted DESC") or trigger_error('Query failed: '. mysql_error());
$r = mysql_fetch_row($result);
$numrows = $r[0];

$rowsperpage = 10;
$totalpages = ceil($numrows / $rowsperpage);

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   $currentpage = (int) $_GET['currentpage'];
} else {
   $currentpage = 1;
}

if ($currentpage > $totalpages) {
   $currentpage = $totalpages;
}
if ($currentpage < 1) {
   $currentpage = 1;
}

$offset = ($currentpage - 1) * $rowsperpage;

$result = mysql_query("SELECT * FROM News ORDER BY Posted DESC LIMIT $offset, $rowsperpage") or trigger_error('Query failed: '. mysql_error());

while ($list = mysql_fetch_assoc($result)) {
      echo '<h4 style="margin:0px; font-size:12px;">'. $list['Artname'] .'</h4>';
      echo '<h4 style="margin:0px; font-size:11px;">'. $list['Posted'] .'</h4>';      
      echo '<p style="margin:0px;">'. $list['Article'] .'</p><br />';
}
echo 'Pages: ';

$range = 3;

if ($currentpage > 1) {
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   $prevpage = $currentpage - 1;
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
}
else
{
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   $prevpage = $currentpage - 1;
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><</a> ";
}

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   if (($x > 0) && ($x <= $totalpages)) {
      if ($x == $currentpage) {
         echo " [<b>$x</b>] ";
      } else {
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      }
   } 
}
                         
if ($currentpage != $totalpages) {
   $nextpage = $currentpage + 1;
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> <br /><br /><br />";
}
else
{
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>></a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> <br /><br /><br />";
}

 

Whats the problem?

It shouldn't display 4 pages, when only 1 has results on. :(

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

I think this

$r = mysql_fetch_row($result);
$numrows = $r[0];

should just be

$numrows = mysql_num_rows($result);

What you had before meant to get the first column of the first returned row from the result set. And i doubt that is the number of rows returned.

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.