Solved

Php Mysql Pagination

Posted on 2012-03-12
1
383 Views
Last Modified: 2012-06-27
I'm trying to display a photo gallery that puts the photos in rows of three, with a total of three rows a page (9 pictures a page).

The navigation in the script moves to page two, but the pictures do not change.  Also, I'm unable to navigate past page two, even though there are 12 pages.

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/includes/header.php");
require_once("includes/sqlconnect.php");
?>
<div id="interiorpages">

<span class="pagetitle">Photo Gallery</span><br />
<div id="sectionunderline"></div><br />

<?php

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

if ($pageno > $lastpage)
      {
    $pageno = $lastpage;
      }

if ($pageno < 1)
      {
   $pageno = 1;
      }
      

$sql = "SELECT id, filename, category, width, height, filesize, caption, category, fileext, hidden FROM photos WHERE hidden = '0' ORDER BY width DESC";
$res = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($res);

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

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$sql2 = "SELECT id, filename, category, width, height, filesize, caption, category, fileext, hidden FROM photos WHERE hidden = '0' ORDER BY width DESC $limit";

$res2 = mysql_query($sql2) or die(mysql_error());
$num2 = mysql_num_rows($res2);

if ($pageno == 1)
      {
      echo " FIRST PREV ";
      }

else
      {
      echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
      $prevpage = $pageno-1;
      echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
      }
      
echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage)
      {
      echo " NEXT LAST ";
      }

else
      {
      $nextpage = $pageno+1;
      echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
      echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
      }

echo '<table width="960px" cellspacing="2" cellpadding="2" border="0" align="center">';

$count = 0;

for ($a = 0; $a < $num2; $a++)
      {
       echo '<tr>';
     for($count=0;$count<3;$count++)
        {
            $id = mysql_result($res2,$a,0);
            $filename = mysql_result($res2, $a, 1);
            $category = mysql_result($res2, $a, 2);
            $w = mysql_result($res2, $a, 3);
            $h = mysql_result($res2, $a, 4);
            $caption = mysql_result($res2, $a, 6);
            $category = mysql_result($res2, $a, 7);

            $width = ($w/8);
            $height = ($h/8);
            
            if ($w == '287')
                  {
                  $width = ($w * 0.84);
                  }
                  
            if ($w == '640')
                  {
                  $width = ($w/2.7);
                  }
                  
            if ($w == '960')
                  {
                  $width = ($w/4);
                  }
                  
            if ($w == '1315')
                  {
                  $width = ($w/5.6);
                  }
                  
            if ($w == '1363')
                  {
                  $width = ($w/5.7);
                  }

            if ($w == '1504')
                  {
                  $width = ($w/6.3);
                  }
                  
            if ($w == '1536')
                  {
                  $width = ($w/6.4);
                  }
                  
            if ($w == '1600')
                  {
                  $width = ($w/6.7);
                  }
                  
            if ($w == '2048')
                  {
                  $width = ($w/8.5);
                  }
                  
            if ($w == '2448')
                  {
                  $width = ($w/10.2);
                  }
                  
            if ($w == '3264')
                  {
                  $width = ($w/13.6);
                  }
                  
            
            else
                  {
                  $width = ($w / 7);
                  }
            
            
          echo '<td>
                         <a href="viewpicture.php?id='.$id.'" class="iframe"><img src="images/gallery/' . $filename . '.jpg"
                         width="'.$width.'px" border = "0"> </span></a><br /><br />
                         ' .$caption . '
                  </td>';
                  
           $a++;
        }
         echo '</tr>';
        $a--;
    }
      
      
echo '</table>';

require_once($_SERVER['DOCUMENT_ROOT']."/includes/footer.php");?>
0
Comment
Question by:t3chguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37712675
I cannot really follow the code posted here, but if the question is about PHP pagination you will find the canonical article here:
http://www.sitepoint.com/perfect-php-pagination/

HTH, ~Ray
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question