problem with image gallary script!@Q%Q#%&

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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

problem with image gallary script!@Q%Q#%&

Post by dull1554 »

i know what has to be done to get this to work but i dont know how to do it,

Code: Select all

<?php 
$max_per_page=10; 
$start_from=$_GET['start_from']; 
$folder = "images/";  //folder name 
if($handle = opendir($folder))  //open the folder 
{ 
     $i=0; 
    while($files = readdir($handle))  // $files will loop through every file in the directory 
  { 
     if ($files != "." && $files != "..")  // readdir returns "." and ".." but they arent' actual files, so strip them out 
    { 
         if($i<$start_from) continue; 
         elseif ($i<$start_from+$max_per_page) break; 
         else echo "<center><img src=".$folder.$files."></center><br>"; 
    } 
  } 
} 
echo " 
<ahref='index.php?start_from=".$start_from-$max_per_page."'> 
Prev</a> 
<ahref='index.php?start_from=".$start_from+$max_per_page."'> 
Next</a>";


what it is that i think has to be done is, well $i is the # of images in the given directory, somehow $i has to increase by one for every picture in the folder.

but heres another problem, i tried to do this
PHP:

Code: Select all

$i = 17;



i set $i to the number of pictures in the given directory, but to no avail , the page displayed all 17 pictures and the

Code: Select all

0'&gt;Next

i really hope i can get this to work, and please if anyone has a soultion it would be greatly appreciated.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Code: Select all

<?php
$max_per_page=10;
$start_from=$_GET['start_from'];
$folder = "images/";  //folder name
if($handle = opendir($folder))  //open the folder
{
    $i=1;
    while($files = readdir($handle))  // $files will loop through every file in the directory
  {
     if ($files != "." && $files != "..")  // readdir returns "." and ".." but they arent' actual files, so strip them out
    {
         if ($i >= $start_from && $i < $start_from + $max_per_page) {
              echo "<center><img src=".$folder.$files."></center><br>";
         }
    }
    $i++; // to increment $i every file.
  }
}
echo "
<a href='index.php?start_from=".$start_from-$max_per_page."'>
Prev</a>
<a href='index.php?start_from=".$start_from+$max_per_page."'>
Next</a>"; 
?>
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

ok i understand that so it adds 1 to $i for every pic in the directory, bun now how do i get it to show only ten pics at a time?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

if ($i >= $start_from && $i < $start_from + $max_per_page) {
echo "<center><img src=".$folder.$files."></center><br>";
}

This check will ensure that only 10 images will show.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

but i don't understand why it's not working, if i pu 30 images in my images directory, 1 it displays all thirty, and 2 it does not give me a link to the next set, i'm totally confused
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

well thanks for all the help everyone, i just thought i'd let you know that i got it to work just fine, i did this

Code: Select all

<?php

//configuration
$max_per_page=10;
$folder = "images/";
//end configuration

if(!isset($_GET['start_from']))
{
    $start_from = "0";
}
else{
    $start_from=$_GET['start_from'];
}

$back = $start_from-$max_per_page;
$forward = $start_from+$max_per_page;
Print <<< EOT
<html>

<head>
<link rel="stylesheet" type="text/css" href="../../styles.css"/>
<script type="text/javascript" src="../../fader.js"></script>
</head>

<center>
<a href=index.php?start_from=$back>back</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href=index.php?start_from=$forward>forward</a></center>

EOT;

if($handle = opendir($folder))
{
    $i=0;
    while($files = readdir($handle))
  {
     if ($files != "." && $files != ".." && $files != "Thumbs.db")
    {
         if ($i >= $start_from && $i < $start_from + $max_per_page) {
              echo "<center><img src=".$folder.$files."></center><br>";
         }
         $i++;
    }

  }
}

Print <<< EOT
<html>

<head>
<link rel="stylesheet" type="text/css" href="../../styles.css"/>
<script type="text/javascript" src="../../fader.js"></script>
</head>

<center>
<a href=index.php?start_from=$back>back</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href=index.php?start_from=$forward>forward</a><br>There are $i pictures in this gallary.</center>

EOT;
?>
i had to use heredoc because for some reason when i used echo it returned

Code: Select all

0'&gt;forward
instead of the links to go forward or backwards.
Post Reply