Question

Hey all, my first post :D

Problem:

I'm trying to make a template gallery, not a slide show, which i can easily reuse on multiple sites.
Mostly for quick folio sites, where the owner wont know how to update the code to add pictures.

It needs to read all of the image files from a selected directory. (jpg, gif, png, bmp) It needs to be able to update content without any code changes. (dynamic load from folder) It needs to write out img tags to the viewed page. (using JavaScript for customization/css?)

The set of img tags output from the php/JavaScript need to be thumbnails which when clicked will link to the full def pictures, this can probably be handled with JavaScript when making the links initially.

Progress:

I found a php script that will read the files from a folder and save the urls to an array for use in JavaScript. However the code used to display the picture is done as a single block slide show, where as i need it to post all images separately not just replace the src of the same image.

Example:

root/index.htm - pastebin[.]com/m52568ed5
root/images/getimages.php - pastebin[.]com/f5395a572
root/images/pic01.png
root/images/pic03.jpg
root/images/asdfs.gif

So how do i get JavaScript to loop through the galleryarray[curimg] and write out my links?

I got this far, and got stuck.

function rotateimages(){
 // document.getElementById("slideshow").setAttribute("src", "res/gallery/painting/"+galleryarray[curimg])
 // curimg=(curimg<galleryarray.length-1)? curimg+1 : 0
 for (curimg=1;curimg!=0;curimg++;) {
 document.write("<div><img class='gallery' src='" + galleryarray[curimg] + "' /></div>")
 }
}

Thanks in advance, Braden.


EDIT: heres my sandbox to show whats going on

-EDIT: removed link

No matter how i change the output per item for example if i replace the whole section with a simple echo all i ever get is the following:

<!DOCTYPE html>
<html>
    <head>
        <title>My Gallery</title>
    </head>

    <body>
        <div id="gallery"></div>
    </body>
</html>

Seems like it gets stuck when it tries to run the 'foreach()'

Heres the current php:

<?php

function getDirTree($dir,$p=true) {
    $d = dir($dir);$x=array();
    while (false !== ($r = $d->read())) {
        if($r!="."&&$r!=".."&&(($p==false&&is_dir($dir.$r))||$p==true)) {
                $x[$r] = (is_dir($dir.$r)?array():(is_file($dir.$r)?true:false));
        }
    }

    foreach ($x as $key => $value) {
        if (is_dir($dir.$key."/")) {
                $x[$key] = getDirTree($dir.$key."/",$p);
        }
    }

    ksort($x);
    return $x;
}

$tree = getDirTree("./res/gallery/");

echo '<div id="gallery">';

foreach($tree as $element => $eval) {
    if (is_array($eval)) {

        foreach($eval as $file => $value) {
                if (strstr($file, "jpg")) {
                        $file = 'res/gallery/'.$element.'/'.$file;
                        echo 'test'; //test//echo '<a href="'.$file.'">test</a>'; //test// <img class="gallery" src="'.$file.'" alt="'.$file.'"/></a>';
                }
        }


    }
}
echo '</div>';

considering as i have Never done php before i started this, i think im doing ok.

Was it helpful?

Solution

Very simple auto gallery script, photos.php:

<?php
function getDirTree($dir,$p=true) {
    $d = dir($dir);$x=array();
    while (false !== ($r = $d->read())) {
        if($r!="."&&$r!=".."&&(($p==false&&is_dir($dir.$r))||$p==true)) {
            $x[$r] = (is_dir($dir.$r)?array():(is_file($dir.$r)?true:false));
        }
    }

    foreach ($x as $key => $value) {
        if (is_dir($dir.$key."/")) {
            $x[$key] = getDirTree($dir.$key."/",$p);
        }
    }

    ksort($x);
    return $x;
}

$tree = getDirTree("./foto/");

echo '<div id="gallery">';
echo '<ul class="linone">';
foreach($tree as $element => $eval) {
    if (is_array($eval)) {
        echo '<li><h4>'.$element.'</h4>';
        echo '<ul class="linone photos">';
        foreach($eval as $file => $value) {
            if (strstr($file, "jpg")) {
                $file = 'foto/'.$element.'/'.$file;
                echo '<li><a href="'.$file.'"><img src="'.$thumb.'" alt="'.$thumb.'"/></a></li>';
            }
        }
        echo '</ul>';
        echo '</li>';
    }
}
echo '</ul>';
echo '</div>';

Also I use the lightbox jQuery plugin to make this gallery comfortable to view.

And also managing photos for this page is very-very simple - you just need to upload .jpg files to your photos directory ('/foto/', for this example).

index.php:

<!DOCTYPE html>
<html>
    <head>
        <title>My Gallery</title>
    </head>

    <body>
        <?php require_once('photos.php') ?>
    </body>
</html>

This file will include photos.php file and runs it, output of photos.php script will come between tags.

OTHER TIPS

You can use PHP to write out the img tags.

Also, your for loop will run infinitely, because it has an end condition which is always true.

EDIT: Looking at your pastebin, it looks like you've misunderstood how PHP works. <script src="res/getimages.php"></script> will not work, as script tags are read on the client side. PHP is run on the server side. To run res/getimages.php, you'll have to do something like:

<?php //This is a PHP opening tag; anything after this is PHP code
include('res/getimage.php'); //Imports the PHP file into index.php
?> <!--this is the closing PHP tag - anything after this is HTML -->

I would highly recommend working through a quick tutorial, such as the W3Schools PHP Tutorial, which will really help you through this. Same for JavaScript. You can keep asking questions about how this all works, but you'll never really understand it, and won't be able to work out how to put all the bits together. Just give it a few hours!

Thanks to everyone who helped out, a huge thanks to Sergey Kunetsov for his gallery script which after slight modification worked perfectly.

and for anyone who wants it, heres the final working php.

http://pastebin.com/f442f31f3

the folder that get displayed is the $path.

Enjoy, and thanks again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top