Page 1 of 1

Pagination not working..

Posted: Thu Jul 01, 2010 4:23 pm
by Bjeffries111
I have tried so many pagination scripts I cannot even count them an longer. So here I am with a very basic script that is also not working.

Everything appears to the title and description then nothing after that. Can anyone see if I am doing something wrong or possibly missing something. I have been working on this now for a week.

Here is my code:

Code: Select all

<?php
//db connector
$aid = mysql_real_escape_string($_GET['aid'], $con);
// create an array to set page-level variables
$page = array();
$page['title'] = '';
/* once the file is imported, the variables set above will become available to it */

// include the page header
include('includes/template/header.php');
//$aid = '1';

//page starts here
echo "<div align='center'>";
echo "<div class='wrapper_photo'>";
//Title and description
$select1 = ("SELECT * FROM photo_albums WHERE aid = '$aid' ");
$result1 = mysql_query($select1) or die(mysql_error());
while($row = mysql_fetch_array($result1)) {
	echo "<h3 align='left'>".$row['atitle']."</h3>"
         ."<p align='left'>".$row['description']."</p>";
}
//Everything works up to here.. then I get nothing..


// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM pictures WHERE aid = $aid";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$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;

$sql = "SELECT * FROM pictures WHERE aid = $aid ORDER BY pid ASC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

//gallery thumbs
//$select = ("SELECT * FROM pictures WHERE aid = '$aid' ORDER BY pid ASC ");
//$result = mysql_query($select) or die(mysql_error());

echo "<div class='photoWrap'>";

while ($list = mysql_fetch_assoc($result)) {

echo "<div class='photoThumb'>"
   ."<a href='".$row['filepath'].$row['filename']."'rel='lightbox[".$aid."]'>"
   //."<a href='".$filename."'>"
   ."<img border='0' width='100px' src='".$row['filepath'].$row['filename']."' />"
   //."<img src='".$filename."' rel='lightbox['".$aid."']' border='0' width='100px' />"
   ."</a><br/></div>";
}
echo "</div>";

/******  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 ******/

echo "</div>";
echo "</div>";
echo "</div>";
// include the page footer
include('includes/template/footer.php');
mysql_close($con);
?> 

Re: Pagination not working..

Posted: Fri Jul 02, 2010 2:07 am
by manRay
Check this video out http://www.youtube.com/watch?v=wC0uc_TkdR0. It is real basic, but it is a good start.