Solved

Php Mysql Pagination

Posted on 2012-03-12
1
379 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
1 Comment
 
LVL 109

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

808 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