Solved

Setting conditional regions on PHP slideshow

Posted on 2004-09-23
13
261 Views
Last Modified: 2013-12-12
The following thread contains a neat little PHP function that will allow a slide show of pictures contained within a directory. The problem is that the first picture displays the "previous" link as well as the last picture displaying "next" The thread is located at http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_20860190.html?query=php+slide+show&clearTAFilter=true

My question is how to get those links to create either a loop, so that clicking "previous" while viewing the first pic will take you to the last one OR how to set a conditional region on those links if they happen to be on the first or last picture.

Here's my current code:

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

$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 "<center><img src=\"slideshow/" . $image[$key] . "\">\n<br>\n<br>\n";
echo "<a href=\"" . $PHP_SELF . "?next=" . $prev_id . "\" class=mainlink>Previous Image</a>\n";
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
echo "<a href=\"" . $PHP_SELF . "?next=" . $next_id . "\" class=mainlink>Next Image</a>\n";
echo "</center>\n";
0
Comment
Question by:cbeaudry1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 6

Expert Comment

by:peyox
ID: 12139883
$key - actual picture
$x - number of pictures

INIFINITE LOOP:
Simple coditional statements will do the job:

$prev_id = $key>1 ? $key-1 : $x;
$next_id = $key<$x ? $key+1 : 1;

BLOCKING PREVIOUS/NEXT LINKS:

if ($prev_id>0) // added line
      echo "<a href=\"" . $PHP_SELF . "?next=" . $prev_id . "\" class=mainlink>Previous Image</a>\n";
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
if ($next_id<=$x)  // added line
      echo "<a href=\"" . $PHP_SELF . "?next=" . $next_id . "\" class=mainlink>Next Image</a>\n";
echo "</center>\n";
0
 
LVL 6

Expert Comment

by:peyox
ID: 12139891
Also you may want to add code for FIRST/LAST image:

echo "<a href=\"" . $PHP_SELF . "?next=1\" class=mainlink>First Image</a>\n";

echo "<a href=\"" . $PHP_SELF . "?next=" . $x . "\" class=mainlink>Last Image</a>\n";
0
 

Author Comment

by:cbeaudry1
ID: 12140141
I'm more of a newbie at PHP. I usually program in ASP...

I'm running into a problem where the first image will only show up when there's no querystring at the end of the URL. Otherwise, I get a broken link that doesn't have an image file name when I hit the first image.

The loop works fine except when it gets to that first image.

Do I need to assign specific values to $key and $x?
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 6

Accepted Solution

by:
peyox earned 100 total points
ID: 12140216
Ok, here's the complete script (revised, reubilded):

<?
      $dir = @opendir("./slideshow");
      $noOfImages = 0;

      while( $file = @readdir($dir) )
            if( !preg_match("/^\.$/", $file) && !preg_match("/^\.\.$/", $file) )
                  $image[++$noOfImages] = $file;
      
      @closedir($dir);
      
      $imageid = ( isset($_GET['image']) ) ? $_GET['image'] : 1;


      echo "<center>";

      if ($imageid>1)
      {
            echo "| <a href=\"" . $PHP_SELF . "?image=1\" class=mainlink>First Image</a> | ";
            echo "<a href=\"" . $PHP_SELF . "?image=" . ($imageid-1) . "\" class=mainlink>Previous Image</a> | ";
      }
      else
      {
         echo "First Image | ";
         echo "Previous Image | ";
      }
        
      if ($imageid<$noOfImages)
      {
            echo "<a href=\"" . $PHP_SELF . "?image=" . ($imageid+1) . "\" class=mainlink>Next Image</a> | ";
            echo "<a href=\"" . $PHP_SELF . "?image=" . $noOfImages . "\" class=mainlink>Last Image</a> |";
      }
      else
      {
         echo "Next Image | ";
         echo "Last Image |";
      }

      echo "<br><br>";
      echo "<img src=\"slideshow/" . $image[$imageid] . "\">\n<br>\n<br>";
      echo "</center>";

?>
0
 

Author Comment

by:cbeaudry1
ID: 12141952
Works like a charm. Thanks!
0
 
LVL 3

Expert Comment

by:danomatic
ID: 12156537
peyox:

 I have this working but after the second image, all I get are broken image icons.

I know this question is closed so I am willing to open a new question to trouble shoot this.

here is my URL:

http://www.akersdesignrender.com/photoscript.php 

I have all the images in a folder call "slideshow"

-- Dan
0
 
LVL 6

Expert Comment

by:peyox
ID: 12156554
Hi Dan,

How does your directory structure looks like?
You probably have folder(s) in it.

Try to put all images in one folder, without subfolder, because that simple function doesn't check whenever "file" is a folder or not.

Let me know if you have any problems.

Cheers,
peyox
0
 
LVL 3

Expert Comment

by:danomatic
ID: 12156699
This is what I have:

/slideshow/name_of_image.jpg (no sub folders here)

/photoscript.php/slideshow/name_of_image.jpg

/ = root of site
0
 
LVL 6

Expert Comment

by:peyox
ID: 12156906
Add this line
   print_r ($image);

after
  @closedir($dir);

I want to see have these files' names look like.

What is "C" in your folder?
http://www.akersdesignrender.com/slideshow/C

Let me know when ready
0
 
LVL 3

Expert Comment

by:danomatic
ID: 12157117
ok, try it now.  I am not sure what you mean by "C"

http://akersdesignrender.com/photoscript.php

-- dan
0
 
LVL 6

Expert Comment

by:peyox
ID: 12157200
Ok, I see....
You have register globals ON in your php.ini file. When entering to the script $image variable is already set... this couses the problem.

Change the variable name $image[] to $image_list[] in the following lines:

$image_list[++$noOfImages] = $file;
echo "<img src=\"slideshow/" . $image_list[$imageid] . "\">\n<br>\n<br>";



This will solve the problem.
I would also recommend you to turn off register global in php.ini!

Cheers,
peyox
0
 
LVL 6

Expert Comment

by:peyox
ID: 12157208
Article about register globals... and related problems :)

http://us4.php.net/register_globals

--
peyox
0
 
LVL 3

Expert Comment

by:danomatic
ID: 12157282
yes that works!

I have no control over the php.ini file, it's hosted at a commercial hosting company.

thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question