Hi, Im trying to get a simple image gallery working on my site, so far I've managed to make pages to upload the image, make a thumbnail and display the images. But now I'm trying to get those images to show on a number of pages.
I've searched the net and found loads of stuff on pagination, most of which I didn't really understand, and have tried to put them into my page.
Here is my script:
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}
// run query
$results = @mysql_query("SELECT * FROM IMAGES WHERE CAT='images' ORDER BY FILE");
$rows = mysql_num_rows($results);
//This is the number of results displayed per page
$page_rows = 20;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
$results_p = mysql_query("SELECT * FROM IMAGES WHERE CAT='images' ORDER BY FILE $max") or die(mysql_error());
// display results
while($info = mysql_fetch_array( $results_p ))
{
$totalColumns = 4;
$i = 1;
echo "<table width='100%' cellpadding='0' cellspacing='0' align='center'>";
while ($row = @mysql_fetch_array($results)){
if ($i == 1) echo "<tr valign='top'>";
echo "<td width='25%' align='center'>";
$pinfo = pathinfo( $row['FILE'] );
$tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];
echo "<a href='images/" . $row['FILE'] . "' target='_blank'><img src='images/" . $tmb_name . "' border='0'></a></td>";
if ($i == $totalColumns)
{
echo "</tr>";
$i = 0;
}
$i++;
}
echo "</table>";
}
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
I know its probably a mess, Im really new to PHP, and still not quite sure what Im doing :p
So far, all the images show on page one, then I click next, and I get the same thing. It says page two in the address bar, but all of the images display again. Even the links at the bottom say "--Page 1 of 2-- ---- Next -> Last ->>".
Please help!