Page 1 of 1

Simple photo gallery - small change of the code.

Posted: Sun Sep 12, 2010 8:07 pm
by Jitro
I am a fresh PHP programmer. I want to create a simple photo gallery and I even did it, my script displays photos, but there is one problem I cant manage - I do not know how to change the php code to display one photo on one page, so that one can press next button and go to the next photo and so on. Now, my script displays all photos on one page. May anybody tell how to do that ?

Code: Select all

<?php
//silhouettes is a folder containing photos

$a = opendir('silhouettes');
$b = readdir($a);

while ($r = readdir($a)){
//check if $r contain ".jpg"
if(strpos("$r",".jpg")){
echo "<img src='silhouettes/".$r."' width='200' height='240' align='center' /><br />";
echo rtrim($r,".jpg");

echo "<br />";
}
}  

closedir($a);
?>
Thanks

Re: Simple photo gallery - small change of the code.

Posted: Sun Sep 12, 2010 9:06 pm
by Jonah Bron
First, you'll need to check the directory for...

Heck, I'm too tired to explain it. I'll just write it. 8)

Code: Select all

<?php
//silhouettes is a folder containing photos

$img = isset($_GET['img']) ? $_GET['img'] : null;

$a = opendir('silhouettes');
$b = readdir($a);
$got_one = false;

while ($r = readdir($a)){
    if(!strpos("$r",".jpg")) {
        continue;
    }
    if ($img == null) {
        $img = $r;
    }
    if ($img == $r) {
        if (isset($r2)) {
            echo '<a href="?img=' . urlencode($r2) . '">prev</a>';
        }
        echo "<img src='silhouettes/".$r."' width='200' height='240' align='center' /><br />";
        echo rtrim($r,".jpg");
        echo "<br />";
        if ($r = readdir($a)) {
            echo '<a href="?img=' . urlencode($r) . '">prev</a>';
        }
        break;
    }
    $r2 = $r;
}  

closedir($a);
?>
That ought to work. Let us know if it gives any errors.

Re: Simple photo gallery - small change of the code.

Posted: Mon Sep 13, 2010 7:55 pm
by Jitro
Yes it works fine. Thank you; and you gave me some material to work on :). So I must look through it carefully.