Next Previous Links for PHP Photo Gallery Script

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
nightmatrix
Forum Newbie
Posts: 21
Joined: Sun Jun 08, 2003 11:03 am

Next Previous Links for PHP Photo Gallery Script

Post by nightmatrix »

Hi My name is Andrew

I am having newbie trouble trying to add next previous links to my script (well it's not my script) if anyone can help it would be much appreciated

Code: Select all

<?php
$a = '0';
$filepath = "thumb";
$url_path = "bw";
$dir = dir($filepath);

echo "<table width="100%" border="0" cellpadding="5" cellspacing="5">";

while($entry=$dir->read()) {
    if($entry == "." || $entry == "..") { 
        continue; 
    } 
    $fp = @fopen("$filepath/$entry","r");
if ($a == '0') {echo "<tr>";}
if ($a == '5') {echo "<tr>";}
if ($a == '10') {echo "<tr>";}
if ($a == '15') {echo "<tr>";}
?><td>
  <a href="javascript:location='bw.php'; window.open('<? echo "$url_path/$entry" ?>', 'characters', 'HEIGHT=480, WIDTH=640, scrollbars=no')">
  <img src="<? echo "$filepath/$entry" ?>" alt="<? echo $entry ?>"></a>
  </td>
  
  <?
$a = $a + 1;
}
?> 
?>
"If I posted this script wrong please let me know"

Thanks in Advance

Andrew
?>
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Re: Next Previous Links for PHP Photo Gallery Script

Post by delorian »

Well, could you tell where you want to add those next, previous links. As far as understand your script, it displays pictures from a folder 5 in every row. I rewrited this script:

Code: Select all

<?php
  $a = '0'; 
  $filepath = "thumb";
  $url_path = "bw";
  $dir = dir($filepath);

  echo "<table width="100%" border="0" cellpadding="5" cellspacing="5">";

  while($entry=$dir->read()) {
      if($entry != "." && $entry != "..") { 
      // $fp = @fopen("$filepath/$entry","r"); ---- this is unecessary
           if (!($a%5)) {echo "<tr>";}
?>
  <td>
  <a href="javascript:location='bw.php'; window.open('<?php echo "$url_path/$entry" ?>', 'characters', 'HEIGHT=480, WIDTH=640, scrollbars=no')">
  <img src="<?php echo "$filepath/$entry" ?>" alt="<?php echo $entry ?>"></a>
  </td>
  
<?php
  
  $a++;
  if (!($a%5) && $a!=0) { echo "</tr>"; }

 } // end of if
} // end of while
?>
nightmatrix
Forum Newbie
Posts: 21
Joined: Sun Jun 08, 2003 11:03 am

Post by nightmatrix »

I would like to put the links below the images

Thanks Andrew
nightmatrix
Forum Newbie
Posts: 21
Joined: Sun Jun 08, 2003 11:03 am

I see now

Post by nightmatrix »

I see what your saying now it displays every image in the folder with five in every row I didn't realize that

I guess what I am looking for is to be able to Display a maximum of 20 pictures 5 in each row 4 columns and have sub folders from the main directory that I can link to another 20 images maybe it would make more sense to choose it by numbers instead of next previous links I can do this manually in HTML

Thats probably easier

Anyway

Thank You

Andrew
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

Hmmm... try this:

Code: Select all

<?php 
  $a = 0; 
  $filepath = "thumb"; 
  $url_path = "bw"; 
  $dir = dir($filepath); 
  $start=0;
  $stop=20;

 if(!empty($_GET['start']) && !empty($_GET['stop'])) {
   $start=intval($_GET['start']);
   $stop=intval($_GET['stop']);
 }


  echo "<table width="100%" border="0" cellpadding="5" cellspacing="5">"; 

  while($entry=$dir->read()) { 
   if($a>=$start && $a<$stop) { // $a<$stop because from 0-19 you have 20 pictures
      if($entry != "." && $entry != "..") { 

           if (!($a%5)) {echo "<tr>";} 
?> 
  <td> 
  <a href="javascript:location='bw.php'; window.open('<?php echo "$url_path/$entry" ?>', 'characters', 'HEIGHT=480, WIDTH=640, scrollbars=no')"> 
  <img src="<?php echo "$filepath/$entry" ?>" alt="<?php echo $entry ?>"></a> 
  </td> 
  
<?php 
  
  $a++; 
  if (!($a%5) && $a!=0) { echo "</tr>"; } 

  } // end of  if($entry 
 } // end of  if($a>=
} // end of while 
?> 
</table>
<?php

 if($start>=20) {
  echo "<a href="".$PHP_SELF."?start=".$start-20."&stop=".$start."">previous</a>";
 }
 echo "&nbsp;";
 echo "<a href="".$PHP_SELF."?start=".$stop."&stop=".$stop+20."">next</a>";


?>
I'm sorry I didn't check this script, but I don't have the access to my serwer from where I write this post. But it should be good.

Of course it's far from good php code.
nightmatrix
Forum Newbie
Posts: 21
Joined: Sun Jun 08, 2003 11:03 am

Post by nightmatrix »

Hey Delorian

Thanks for all your help so far

but there is an error in that script unexpected T_CONSTANT_ENCAPSED_STRING expecting "," or ";"

I tried for a little while to fix this on my own, so I don't feel totally useless

but to know avail the error is on line 45

If you come back to this post any help would be appreciated thanks Andrew
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

a quick fix

Post by phpScott »

I ran Delorian through my editor and it didn't like the $start-20 or $stop+20 code so I tweaked it and placed that little section below. Try it out to see if that will work.

It belongs inside the last set of php tags.

Code: Select all

if($start>=20) &#123;
    $start2=$start-20;
  echo "<a href="".$PHP_SELF."?start=".$start2."&stop=".$start."">previous</a>";
&#125;
echo " ";
$stop2=$stop+20;
echo "<a href="".$PHP_SELF."?start=".$stop."&stop=".$stop2."">next</a>";
phpScott
nightmatrix
Forum Newbie
Posts: 21
Joined: Sun Jun 08, 2003 11:03 am

Post by nightmatrix »

Hey Thanks

It seems to be working fine however it won't display the Images on the second page I thought maybe there had to be 20 more images to display is this correct

Thanks

Andrew
nightmatrix
Forum Newbie
Posts: 21
Joined: Sun Jun 08, 2003 11:03 am

Does'nt work 100% Percent

Post by nightmatrix »

Hey PHP Scott

I tried uploading 20 more images and it still won't display them in the next section do you know how I can fix this


Thanks Andrew
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

Of course it didn't work, because, I've made a mistake (newbee-one) :lol: That's the way you don't test your code, silly me...
How could this work if the $a wasn't increased at all, when the start was >=20.

Here's some update, I aslo add new variable $new_tr which controls adding new <tr> to your table. It should work. I still can't test it, I'm realy sorry.

Code: Select all

<?php 
  $a = 0;
  $new_tr = 0; 
  $filepath = "thumb"; 
  $url_path = "bw"; 
  $dir = dir($filepath); 
  $start=0; 
  $stop=20; 

if(!empty($_GET['start']) && !empty($_GET['stop'])) { 
   $start=intval($_GET['start']); 
   $stop=intval($_GET['stop']); 
} 

  echo "<table width="100%" border="0" cellpadding="5" cellspacing="5">"; 

  while($entry=$dir->read()) { 
   if($a>=$start && $a<$stop) { // $a<$stop because from 0-19 you have 20 pictures 
      if($entry != "." && $entry != "..") { 

           if (!($new_tr%5)) {echo "<tr>";} 
?> 
  <td> 
  <a href="javascript:location='bw.php'; window.open('<?php echo $url_path."/".$entry; ?>', 'characters', 'HEIGHT=480, WIDTH=640, scrollbars=no')"> 
  <img src="<?php echo "$filepath/$entry"; ?>" alt="<?php echo $entry; ?>"></a> 
  </td> 
  
<?php 
  
if (!($new_tr%5) && $new_tr!=0) { echo "</tr>"; } 

  } // end of  if($entry 
} // end of  if($new_tr>= 
 $a++; 
} // end of while 
?> 
</table> 
<?php 

if($start>=20) { 
  echo "<a href="".$PHP_SELF."?start=".$start-20."&stop=".$start."">previous</a>"; 
} 
echo " "; 
echo "<a href="".$PHP_SELF."?start=".$stop."&stop=".$stop+20."">next</a>"; 


?>
nightmatrix
Forum Newbie
Posts: 21
Joined: Sun Jun 08, 2003 11:03 am

Post by nightmatrix »

Everything is Working fine, Except it is'nt displaying it in rows and columns it is just displaying it in one long line

Do you know how to fix this

Thank You

Andrew
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

Here you are, the final version (still not-tested :( ):

Code: Select all

<?php 
  $a = 0; 
  $new_tr = 0; 
  $filepath = "thumb"; 
  $url_path = "bw"; 
  $dir = dir($filepath); 
  $start=0; 
  $interval=20; // how many pictures will be displayed 

if(!empty($_GET['start'])) { $start=intval($_GET['start']); } 

  echo "<table width="100%" border="0" cellpadding="5" cellspacing="5">"; 

  while($entry=$dir->read()) { 
   if($a>=$start && $a<($start+$interval)) { // $a<$start+$interval because from 0-19 you have 20 pictures 
      if($entry != "." && $entry != "..") { 

           if (!($new_tr%5)) {echo "<tr>";} 
?> 
  <td> 
  <a href="javascript:location='bw.php'; window.open('<?php echo $url_path."/".$entry; ?>', 'characters', 'HEIGHT=480, WIDTH=640, scrollbars=no')"> 
  <img src="<?php echo "$filepath/$entry"; ?>" alt="<?php echo $entry; ?>"></a> 
  </td> 
  
<?php 
  $new_tr++;
if (!($new_tr%5) && $new_tr!=0) { echo "</tr>"; } 

  } // end of  if($entry 
} // end of  if($new_tr>= 
$a++; 
} // end of while 
?> 
</table> 
<?php 

if($start>=20) { 
  echo "<a href="".$PHP_SELF."?start=".$start-20."">previous</a>"; 
} 
echo "<a href="".$PHP_SELF."?start=".$start+20."">next</a>"; 


?>
As you can see I've used only start variable. That's better. And, of course now it should display 5 pictures in one row.
nightmatrix
Forum Newbie
Posts: 21
Joined: Sun Jun 08, 2003 11:03 am

Hey Great it works

Post by nightmatrix »

Hey Thanks

It works excellent now

You rock thanks so much

Cheers

Andrew
Post Reply