Link to home
Start Free TrialLog in
Avatar of danomatic
danomaticFlag for United States of America

asked on

Simple PHP array for a folder of JPG's?

I am looking to do a PHP web page that draws from a folder of JPG's.

I think it is some kind of array that will display the photos one after another.  I don't want a "random script".  I have the idea of a "forward" and "back" button and the idea is that the client can always upload new images anytime he wants keeping to a standard of say about 30 images at any given time.  I figured with an array they could be numered like "1_name-a.jpg", 2_name-whatever.jpg", etc..

I don't know alot about PHP so I am pretty new to this.  I do know ASP.  This will be on a server that has PHP 4.  I don't have access to a database though.

Is this possible?

Dan
SOLUTION
Avatar of lozloz
lozloz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of danomatic

ASKER

I think we are on the right track with the code but I also want to add a forward and back button like:

       <a href="#">Previous Image</a> | <a href="#">Next Image</a>  and maybe a small arrow image as well.

I guess if it were ASP it might be:

<a href="#">Previous Image</a> | <a href="#">

Can this HTML be integreated with the PHP array?
I think we are on the right track with the code but I also want to add a forward and back button like:

       <a href="#">Previous Image</a> | <a href="#">Next Image</a>  and maybe a small arrow image as well.

I guess if it were ASP it might be:

<a href="#">Previous Image</a> | <a href="#">

Can this HTML be integreated with the PHP array?
For my ASP, I meant to say:

<a href="<% some ASP code here%>" etc.. I know my syntax is bad...
Avatar of _Timm_
_Timm_

Actually you dont even need to make an array of images if you're wanting to do that..

OK, let's say you have a folder with images numbered :
1.jpg
2.jpg
3.jpg
so on...

We can do this code:

------

<?
// call your image by going to filename.php?image_id=x
// x being the id of the image!

$image_id = $_GET['image_id'];
$prev_id = $image_id--;
$next_id = $image_id++;

echo "<img src=\"image_folder/" . $image_id . ".jpg\">\n<br>\n<br>\n";
echo "<a href=\"" . $PHP_SELF . "?image_id=" . $prev_id . "\">Previous Image</a> | <a href=\"" . $PHP_SELF . "?image_id=" . $next_id . "\">Next Image</a>\n";

?>

-----

Again, this is untested .. Hope it helps..
i'd replace $PHP_SELF with $_SERVER["PHP_SELF"] in the above code if you want to make it compatible with reg_globals off. also you might wanna only print the first link if $image_id > 0. i don't know how you'd find the max image number without reading the entire directory's contents though, perhaps you could do:

if(file_exists("image_folder/" . $next_id . ".jpg")) {
  // print next image
}

cheers,

loz
sorry, I got held up on this project.  I'll post back soon when I can try some of these.  

I have to get to the "paying" clients first.  This is just a project of my own. -- Dan
Hi _Timm_:

I tired your code. The image comes up as broken. My image diectory is

/show/ from root, so I have:

            echo "<img src=\"/show/" . $image_id . ".jpg\">\n<br>\n<br>\n";

This is the source when viewed as HTML when the page is loaded.

           <img src="/show/-2.jpg">

How do I get rid of the -2.jpg?

Oh, here is how the url looks:

www.domianname.com/show/rotate3.php?image_id=
uh oh, don't click on the url. I didn't realize that it actually went somewhere.  (I just made it up)
how are your images named?
The images are all named with misc names.  The idea was that the client could upload "anyname.jpg" and it would be included in the image slide show with the previous and next buttons.
OH, ok...  Let's try this...

this is assuming your images are still in the "show" folder.. and your rotate3.php is in the root..

http://yourdomain.com/rotate3.php

this time i'm calling them with "next"..

http://yourdomain.com/rotate3.php?next=x

<?

$dir = @opendir("./show");

$x = 0;
while( $file = @readdir($dir) )
{
      if( preg_match("/^\.$/", $file) || preg_match("/^\..$/", $file) )
      {
            //do nothing :)
      }
      else
      {
            $image[$x] = $file;
            $x++;
      }
}

@closedir($dir);

$key = ( isset($_GET['next']) ) ? $_GET['next'] : 0;
$prev_id = $key - 1;
$next_id = $key + 1;

echo "<img src=\"show/" . $image[$key] . "\">\n<br>\n<br>\n";
echo "<a href=\"" . $PHP_SELF . "?next=" . $prev_id . "\">Previous Image</a> | <a href=\"" . $PHP_SELF . "?next=" . $next_id . "\">Next Image</a>\n";

?>
small correction:

<?

$dir = @opendir("./show");

$x = 0;
while( $file = @readdir($dir) )
{
      if( preg_match("/^\.$/", $file) || preg_match("/^\.\.$/", $file) )
      {
            //do nothing :)
      }
      else
      {
            $image[$x] = $file;
            $x++;
      }
}

@closedir($dir);

$key = ( isset($_GET['next']) ) ? $_GET['next'] : 0;
$prev_id = $key - 1;
$next_id = $key + 1;

echo "<img src=\"show/" . $image[$key] . "\">\n<br>\n<br>\n";
echo "<a href=\"" . $PHP_SELF . "?next=" . $prev_id . "\">Previous Image</a> | <a href=\"" . $PHP_SELF . "?next=" . $next_id . "\">Next Image</a>\n";

?>
I tried that:

I got this parse error:

parse error in /usr/local/www/virtual3/44/175/55/245/html/rotate3.php on line 58

Line 58 is:

57     if( preg_match("/^\.$/", $file) || preg_match("/^\.\.$/", $file) )
58   {
59          //do nothing :)

show me the rest of your code please... there's nothing wrong with that line.. (or the previous line..)
The code is as above.

<?

$dir = @opendir("./show");

$x = 0;
while( $file = @readdir($dir) )
{
    if( preg_match("/^\.$/", $file) || preg_match("/^\.\.$/", $file) )
    {
         //do nothing :)
    }
    else
    {
         $image[$x] = $file;
         $x++;
    }
}

@closedir($dir);

$key = ( isset($_GET['next']) ) ? $_GET['next'] : 0;
$prev_id = $key - 1;
$next_id = $key + 1;

echo "<img src=\"show/" . $image[$key] . "\">\n<br>\n<br>\n";
echo "<a href=\"" . $PHP_SELF . "?next=" . $prev_id . "\">Previous Image</a> | <a href=\"" . $PHP_SELF . "?next=" . $next_id . "\">Next Image</a>\n";

?>
heh, how is that line 58 then? make sure you have ?> at the end of the file too.. could you give me a link to your file?
Well, I think I have come to a point where I am spending way to much time on this and it’s still not working. Part of the issue is that I don't understand one bit of PHP. Can this be done a little more easily in ASP?
I never liked ASP..

Are you using PHP on a windows server?
I would like to end this question. Clearly there are server problems.  

You all have been helpful so I am going to grade this anyway.  I am going to repost the question in ASP.  

I just don't have time to learn PHP.  It looks completely like a foreign language to me. I am all right-brained so even ASP is a struggle but at least I have a basic grasp on it.
I tried this script and it does the trick, except that the "previous" link leads nowehere on the first picture as well as the "next" link on the last pictures. Those should be taken out with a conditional statement. Not sure how that would be formulated though....
That shouldn't be too hard.. Delete this line:

echo "<a href=\"" . $PHP_SELF . "?next=" . $prev_id . "\">Previous Image</a> | <a href=\"" . $PHP_SELF . "?next=" . $next_id . "\">Next Image</a>\n";


Add this:

$prev_link = ( $key == 0 ) ? " - " : "<a href=\"" . $PHP_SELF . "?next=" . $prev_id . "\">Previous Image</a>";
$next_link = ( $key == max($key) )  ? " - " : "<a href=\"" . $PHP_SELF . "?next=" . $next_id . "\">Next Image</a>";

echo $prev_link . " | " . $next_link;



------



By the way, this code is untested but I'm pretty confident it should work fine..
Timm,

Thanks for adding the comment. A revised version was posted on the following thread and works perfectly:

https://www.experts-exchange.com/questions/21143252/Setting-conditional-regions-on-PHP-slideshow.html