Jump to content

my pagination stopped working


jacko_162

Recommended Posts

I have a page that paginates results, i have a maximum of 6 per page before it goes to page number 2.

 

now if i have enough results to warrent a page 2 it shows the pagination for 2 pages at tghe bottom.

 

however when i click the number "2" i get the following in my URL

 

results.php?query=SELECT * FROM tests WHERE member_id='1' ORDER BY id DESC LIMIT 0, 8&page=8&limit=8

 

Here is my results.php page

<?php
session_start();

include('Includes/auth.php');
require_once('header.php');


$connect = @mysql_connect("$host", "$username", "$password");

if (!($connect)) // If no connect, error and exit().
{
echo("<p>Unable to connect to the database server.</p>");
exit();
}

if (!(@mysql_select_db($database))) // If can't connect to database, error and exit().
{
echo("<p>Unable to locate the $database.</p>");
exit();
}

if (!($limit)){
$limit = 4;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = mysql_query("SELECT * FROM gallery WHERE memberid=$_SESSION[sESS_MEMBER_ID]"); // the query.
    	

$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
include('nogallery.php'); // bah, modify the "Not Found" error for your needs.
exit();}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.

else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.
?>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf8"/>
<title>Index</title>
</head>

<body class="cloudy">
<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
      <tr>
        <td width="40%" valign="top">
        <div align="right"><a href="myaccount.php"><img src="img/buttons/my_account.png" alt="" border="0" /></a><img src="img/spacer.png" alt="" width="10" height="1" /><a href="addgallery.php"><img src="img/buttons/upload_image.png" alt="" border="0" /></a><img src="img/spacer.png" alt="" width="10" height="1" border="0" /></div>
        <div class="content-box column-left">
	<div class="content-box-header">
			<h3>My Gallery</h3>	
	  </div><div class="content-box-content"><div><h4></h4>
                  
				<table width="100%" border="0" cellpadding="0" cellspacing="4">
      <tr>
        <td align="center" valign="top"><div><div>
        <h2>Showing 
						  <strong><?=$first?></strong>
						 - 
						<strong><?=$last?></strong>
						 of 
						<strong><?=$numrows?></strong></h2>
</div>
		  <table width="100%" border="0" cellspacing="4" cellpadding="4">
<?php

$query = "SELECT * FROM gallery WHERE memberid='$_SESSION[sESS_MEMBER_ID]' ORDER BY id DESC LIMIT $page, $limit";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); 
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 4;
    while($row = mysql_fetch_array($result))        
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // reduces month output data to 3 digits
       $month=substr($month,0,3);

       // make sure we have a valid output
       if($id != "" && $id != null)
   
echo "<td><table class='sample' width='85%' border='0' cellspacing='0' cellpadding='6'><tr align='center' height='100%' /><td height='220'><table height='100%' border='0' cellspacing='0' cellpadding='4'><tr><td>";
echo "<strong><span class='medium_text'>{$row['caption']}</span></strong></td></tr>";
//echo "<br />";
echo "<tr><td align='center' valign='middle'><a href='{$row['image']}' rel='facebox'><img class='gallery' src='{$row['thumb']}' /></a></td></tr>";
//echo "<br>";
echo "<tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td><img src='img/icons/date.png' /> {$row['date']}</td><td><a href='remove-confirm.php?ID=<?php echo $encoded ?>' rel='facebox'><img src='img/icons/delete2.png' border='0' alt='Delete these results'/></a></td></tr></table></td></tr></table></td></tr></table>";
echo "</td>";

       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";
}
?>
</tr>
</table>					  
              <div align="center"><br>
                <?php									  
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">« Previous</a>    \n");}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b> \n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
}
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">Next »</a>\n");}
?>
              </div>
        </div></td>
      </tr>
    </table>
        </td>
      </tr>
</table>
<?php
require_once('footer.php');
?>
</body>
</html>

 

i have been editing code and must of changed something previously and forgot to error check this page. but i cant find what i have changed.

 

can someone see if i forgot something or removed something for me as i cant see where to look? many thanks.

Link to comment
https://forums.phpfreaks.com/topic/247192-my-pagination-stopped-working/
Share on other sites

im just checking out:

http://www.phpfreaks.com/tutorial/basic-pagination

 

and following it, so far i understand most of it, is it possible to echo the above tutorial into a grid layout instead of lists?

 

this is my code used from tutorial to provide the page and details;

<?php
session_start();

include('Includes/auth.php');
require_once('header.php');

// database connection info
$conn = @mysql_connect("$host", "$username", "$password");

if (!($conn)) // If no connect, error and exit().
{
echo("<p>Unable to connect to the database server.</p>");
exit();
}

if (!(@mysql_select_db($database))) // If can't connect to database, error and exit().
{
echo("<p>Unable to locate the $database.</p>");
exit();
}



// find out how many rows are in the table 
$sql = mysql_query("SELECT COUNT(*) FROM `gallery` WHERE memberid=$_SESSION[sESS_MEMBER_ID]");

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

// number of rows to show per page
$rowsperpage = 2;
// 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;

// get the info from the db 
$sql = "SELECT id, thumb FROM gallery LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
   // echo data
   echo $list['id'] . " : " . $list['thumb'] . "<br />";
} // end while

/******  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'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</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'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>

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.