Any Ideas?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slater
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2004 12:13 pm
Location: Nottingham, England

Any Ideas?

Post by slater »

Hey,

Ive got the following code for displaying pictures in specifeid folders

Code: Select all

<?
INCLUDE "conf.php3";
if ((!isset($gal))||(!isset($title[$gal]))){
print "<TITLE>Categories</TITLE>";
include "banner.php3";
print "<h1>Categories</h1>";
$i=0;
while (isset($title[$i])){
print "<h2><a href=$PHP_SELF?gal=$i>$title[$i]</a></h2>";
$i++;
};
}else{
?>
<TITLE><? print $title[$gal]; ?></TITLE>
</HEAD>
<BODY BACKGROUND="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000ff" VLINK="#800080" ALINK="#ff0000" >
<?
error_reporting(63);
include "banner.php3";
?>
<center>
<h1><? print $title[$gal] ?></h1>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?
if (!isset($pg)){
$pg=0;
};
$handle=opendir($dir[$gal]);
$i=0;
while ($file = readdir($handle)) {
if (eregi("jpg$",$file)){
$folder[$i]=$file;
$i=$i+1;
};
};
closedir($handle);
sort($folder);
$a=0;
$count=$row*$col*$pg;
for ($r=1; $r<=$row; $r++) {
print "<tr>";
if ($count>$i-1) {break;};
for ($c=1; $c<=$col; $c++) {
$tnpath=$tndir[$gal]."/".$folder[$count];
print "<td valign="center" align="center"><a href="view.php3?id=$folder[$count]&gal=$gal&pg=$pg&count=$count"><img src="$tnpath" BORDER=0></a><td valign="center">"; $count++;
if ($count>$i-1) {break;};
} ;
print "</tr>";
}  ;
print "</table><h2>";
if ($pg>0) {
$tmp=$pg-1;;
print "<a href="$PHP_SELF?pg=$tmp&gal=$gal"><img src="prev.gif" alt="prev" width="30" height="20" border="0"></a>" ;
};
$tmp=$pg+1;
print "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
if ($tmp*$row*$col<$i) {
$tmp=$pg+1;
print "<a href="$PHP_SELF?pg=$tmp&gal=$gal"><img src="next.gif" alt="prev" width="30" height="20" border="0"></a>" ;
};
print "</h2>";
};
include "bannerbot.php3";
?>
What id like to do is when the thumbnails are displayed instead of just having a next and back buton have the page numbers at the bottom e.g. 1 2 3 4 etc etc, any quick and easy solutions?

Cheers
Slater
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

what you need is a pagination script, i think. search the forum and you will get some info :)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Define the max number of images to show per page, then find the total number of images that are to be shown. Divide this number by the max number to be shown per page and round it up to the next whole number using ceil() so:

Code: Select all

<?php
$MAX_NUM = 10;
$total_number = 43;

$pages = ceil($total_number/$MAX_NUM);
//pages equals 5
?>
Then at the bottom/top of the page do a for loop that prints the page numbers with links:

Code: Select all

<?php
               for($x=1;$x<=$pages;$x++){
                  if($x == $page){
                     $text .= "$x ";
                  }else{
                     $text .= "<a href="$link&p=$x"><small>$x</small></a> ";
                  }
               }
?>
Then of course you need to grab $_GET[p] from the url and check it and edit the query to select what images need to be shown. For this code to work properly you should use $page to hold the value of $_GET[p], but as i said, you need to check that value for XSS and to make sure its a valid page - if not set it to 1.
Post Reply