Solved

Php Mysql Pagination

Posted on 2012-03-12
1
376 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 108

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now