Solved

Php Mysql Pagination

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

932 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

16 Experts available now in Live!

Get 1:1 Help Now