Page 1 of 1

About Next Previous Links

Posted: Thu Feb 24, 2005 7:53 pm
by maboroshi
Hi Everyone been a while since I Posted! I am running a very simple gallery script but there is a few errors in the code that I am having problems working out. The first problem is it won't display the correct amount of images. If I want to display 9 images I have to set the variable at 11. Second the next link is always there and continues to count up and up and up

anyhelp with this script would be very much appreciated

Code: Select all

<?php 
  $a = 0; 
  $new_tr = 0; 
  $filepath = "abstractthumbs"; 
  $url_path = "abstract"; 
  $dir = dir($filepath);
  $start=0;
  $stop=11;

if(!empty($_GET&#1111;'start']) && !empty($_GET&#1111;'stop'])) &#123; 
   $start=intval($_GET&#1111;'start']); 
   $stop=intval($_GET&#1111;'stop']); 
&#125; 

  echo "<table width="600" border="0" cellpadding="5" 

cellspacing="5">"; 

  while($entry=$dir->read()) &#123; 
   if($a>=$start && $a<$stop) &#123; // $a<$stop because from 0-19 you have 20 

pictures 
      if($entry != "." && $entry != "..") &#123; 

           if (!($new_tr%3)) &#123;echo "<tr>";&#125; 
?>
  <td align="center" valign="middle"> 
  <a href="abstractpics.php?image=<?php echo urlencode($entry); ?>"> 
 
  <img src="<?php echo "$filepath/$entry"; ?>" alt="<?php echo $entry; ?>" 

border="1" class="imgborder"></a> 
  </td> 
  
<?php 
  $new_tr++; 
if (!($new_tr%3) && $new_tr!=0) &#123; echo "</tr>"; &#125; 

  &#125; // end of  if($entry 
&#125; // end of  if($new_tr>= 
$a++; 
&#125; // end of while 
?>
</td>
</tr>
</table>
<table width="600">
<tr align="center">
<td>

<?php 

if($start>=9) &#123; 
    $start2=$start-9; 
  echo "<a 

href="".$PHP_SELF."?start=".$start2."&stop=".$start."">previous</a>"; 
&#125; 
echo " "; 

if ($stop != 0) &#123;
$stop2=$stop+9; 
echo "<a href="".$PHP_SELF."?start=".$stop."&stop=".$stop2."">next</a>";
&#125;

?>


</td>
</tr>
</table>

Posted: Thu Feb 24, 2005 8:16 pm
by feyd
might want to read the forum rules and stickies in each board...


Moved to PHP - Code.

Posted: Thu Feb 24, 2005 8:48 pm
by smpdawg
Besides a little reformatting so I can read it I made a minor change to your handling of $a. You were incrementing $a even when you got a . or .. and this caused your counts to be off by 2. Now $a is incremented only when the file is in the range you select or isn't in the range but is not a directory identifier.

The next was easy. $a is (in theory) the max number of files in the directory when you are done looping so you just change your test to see if $stop <= $a.

Code: Select all

<?php
$a = 0;
$new_tr = 0;
$filepath = "abstractpath";
$url_path = "abstract";
$start=0;
$stop=9;

if(!empty($_GET&#1111;'start']) && !empty($_GET&#1111;'stop'])) &#123;
    $start=intval($_GET&#1111;'start']);
    $stop=intval($_GET&#1111;'stop']);
&#125;

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

while($entry=$dir->read()) &#123;
    if($a>=$start && $a<$stop) &#123; // $a<$stop because from 0-19 you have 20 pictures
        if(($entry != ".") && ($entry != "..")) &#123;           
            if (!($new_tr%3)) &#123;echo "<tr>";&#125;
            ?>
            <td align="center" valign="middle">
            <a href="abstractpics.php?image=<?php echo urlencode($entry); ?>">
            <img src="<?php echo "$filepath/$entry"; ?>" alt="<?php echo $entry; ?>" border="1" class="imgborder"></a>
            </td>           
            <?php
            $new_tr++;
            if (!($new_tr%3) && $new_tr!=0) &#123; echo "</tr>"; &#125;           
            $a++;
        &#125; // end of  if($entry
    &#125; else &#123; // end of  if($new_tr>=
      $a++;
    &#125;
&#125; // end of while
?>
</td>
</tr>
</table>
<table width="600">
<tr align="center">
<td>

<?php
if($start>=9) &#123;
    $start2=$start-9;
    echo "<a href="".$_SERVER&#1111;'PHP_SELF']."?start=".$start2."&stop=".$start."">previous</a>";
&#125;
echo " ";

if ($stop <= $a) &#123;
    $stop2=$stop+9;
    echo "<a href="".$_SERVER&#1111;'PHP_SELF']."?start=".$stop."&stop=".$stop2."">next</a>";
&#125;
?>
</td>
</tr>
</table>