Jump to content

difficult to add page numbers to this pagination ?


Reaper0167

Recommended Posts

I was looking to add page numbers to this script.(Special thanks to everyone that helped me with this script) I'm looking to take it a step further and add some page numbers, so a user doesn't have to keep clicking the next button to get to, lets say picture 15 of 60. I've done a few searches and there really isn't a detailed example. Most of the tutorials are hard to follow. Plus, I would like to pretty much keep the general idea of this script, if you know what I mean. Any suggestions.

<?php
include 'connection.php';
if (isset($_GET['pageno']))
{
   $pageno = (int)$_GET['pageno'];  //added int
}
else
{
   $pageno = 1;
}
$query = "SELECT count(*) FROM abc123 WHERE category = '$selecting'";
$result = mysql_query($query);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
$rows_per_page = 10;
$lastpage = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage)
{
   $pageno = $lastpage;
}
if ($pageno < 1)
{
   $pageno = 1;
}
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT id, thumb_1, item_name, description, in_return FROM abc123 WHERE category = '$selecting' $limit";
$result = mysql_query($query);
if($lastpage != 0)
{
   echo "<table border='0' CELLPADDING=5 STYLE='font-size:16px'>";
   while ($row = mysql_fetch_array($result))
   {
      echo "<tr><td align='center'>";
  echo '<a href="viewitem.php?sendto='.$row['id'].'"><img src="' . $row['thumb_1'] . '" border="0" alt=""></a>';
  echo "</td><td align='center'>";
  echo "".substr($row['item_name'],0,50),"";
  echo "</td><td align='center'>";
  echo substr($row['description'],0,50).'<a href="viewitem.php?sendto='.$row['id'].'" class="view_item"> ...more</a>';
  echo "</td><td align='center'>";
  echo substr($row['in_return'],0,50).'<a href="viewitem.php?sendto='.$row['id'].'" class="view_item"> ...</a>';
  echo "</td><td align='center'>";
  echo "</td></tr>";
      echo '<tr><td height="19" colspan="4">';
      echo '<hr width="550">';
      echo "</td></tr>";
   }
   echo "</table>";
   if ($pageno != 1)
   {
   	  $prevpage = $pageno-1;
   	  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREVIOUS</a> ";
   }
   echo " ( Page $pageno of $lastpage ) ";
   if ($pageno != $lastpage)
   {
      $nextpage = $pageno+1;
      echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
   }
}
else
{
   echo 'No pictures.';
}
mysql_close();
?>

Link to comment
Share on other sites

If $lastpage contains the number of the last page you could have a loop at the bottom of your page like so...

 

 

for($i=2;$ i<$lastpage; $i++)
{
    echo "<a href='{$_SERVER['PHP_SELF']}?pageno=$i'>$i</a>";
}

 

I set $i as 2 so you would only display the page numbers if their was more than 2 pages..

Link to comment
Share on other sites

Well, I just noticed that my original pagination setup does not work properly. So before I got and add page numbers, I think it would be best to fix it. If I set my rows per page to 1, it says that I have 3 pages, which is correct, I have 3 pictures in the database. But when I click on the next button, it then says that I have no pics. Anyone know what is going on?

<?php
   include 'connection.php';
   if (isset($_GET['pageno']))
   {
     $pageno = (int)$_GET['pageno'];
   }
   else
   {
     $pageno = 1;
   }
   $query = "SELECT count(*) FROM abc123 WHERE category = '$selecting'";
   $result = mysql_query($query);
   $query_data = mysql_fetch_row($result);
   $numrows = $query_data[0];
   $rows_per_page = 1;
   $lastpage = ceil($numrows/$rows_per_page);
   $pageno = (int)$pageno;
   if ($pageno > $lastpage)
   {
     $pageno = $lastpage;
   }
   if ($pageno < 1)
   {
     $pageno = 1;
   }
   $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
   $query = "SELECT id, thumb_1, item_name, description, in_return FROM abc123 WHERE category = '$selecting' $limit";
   $result = mysql_query($query);
   if($lastpage != 0)
   {
     echo "<table border='0' CELLPADDING=5 STYLE='font-size:16px'>";

     while ($row = mysql_fetch_array($result))
 {
       echo "<tr><td align='center'>";
   echo '<a href="viewitem.php?sendto='.$row['id'].'"><img src="' . $row['thumb_1'] . '" border="0" alt=""></a>';
   echo "</td><td align='center'>";
   echo "".substr($row['item_name'],0,50),"";
   echo "</td><td align='center'>";
   echo substr($row['description'],0,50).'<a href="viewitem.php?sendto='.$row['id'].'" class="view_item"> ...more</a>';
   echo "</td><td align='center'>";
   echo substr($row['in_return'],0,50).'<a href="viewitem.php?sendto='.$row['id'].'" class="view_item"> ...</a>';
   echo "</td><td align='center'>";
   echo "</td></tr>";
       echo '<tr><td height="19" colspan="4">';
       echo '<hr width="550">';
       echo "</td></tr>";
     }
 echo "</table>";
 if ($pageno != 1)
 {
       $prevpage = $pageno-1;
   	   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREVIOUS</a> ";
 }
 echo " ( Page $pageno of $lastpage ) ";
 if ($pageno != $lastpage)
 {
   	   $nextpage = $pageno+1;
   	   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
   	 }
       
  
  }
  else
  {
echo 'You have no pictures.';
  }
  mysql_close();
  ?>

Link to comment
Share on other sites

well the only way you can get to the else statement for "No pictures" is if your last page equals 0. $lastpage is only set in once place which is here. 

 

$lastpage = ceil($numrows/$rows_per_page);

 

Now we know $rows_per_page = 1 so $numrows must be set to zero, try echoing out query_data[0] to see if it contains the correct result on each page....

 

Ben

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.