Simple photo gallery - small change of the code.

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
Jitro
Forum Newbie
Posts: 5
Joined: Sun Sep 12, 2010 7:43 pm

Simple photo gallery - small change of the code.

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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.
User avatar
Jitro
Forum Newbie
Posts: 5
Joined: Sun Sep 12, 2010 7:43 pm

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

Post by Jitro »

Yes it works fine. Thank you; and you gave me some material to work on :). So I must look through it carefully.
Post Reply