PHP implementation to automatically scroll to display the image of the method _php tips

Source: Internet
Author: User
Tags prev sessions

The example of this article describes how the PHP implementation automatically scrolls the display of the picture. Share to everyone for your reference. Specifically as follows:

Specifies a picture directory that automatically scrolls through each picture on the page, using the method:

1. Create an image folder for a slide.
2. Delete the slides in the image folder.
3. After encoding the following code, paste in a text file, named "index.php"
4. Upload files to a directory
5. Replace the 6th and 8 behavior of your corresponding settings.
6. Run (using the URL set in step 4th)

The code is as follows:

Copy Code code as follows:
?
/*
PHP Image Slideshow-auto Version-php5
*/
Set the absolute path to the directory containing the images
Define (' Imgdir ', '/home/devel/public_html/domain.com/public/images/slideshow/');
Same but for www
Define (' Webimgdir ', '/images/slideshow/');
Set session name for slideshow "Cookie"
Define (' Ss_sessname ', ' slideshow_sess ');
Global error variable
$err = ';
Start img Session
Session_name (Ss_sessname);
Session_Start ();
Init Slideshow Class
$SS = new Slideshow ($err);
if ($err = $ss->init ())!= ")
{
Header (' http/1.1 Internal Server Error ');
Echo $err;
Exit ();
}
Get image files from directory
$ss->get_images ();
Set variables, done.
List ($curr, $caption, $first, $prev, $next, $last) = $ss->run ();
/*
Slideshow class, can be used stand-alone
*/
Class Slideshow
{
Private $files _arr = NULL;
Private $err = NULL;
Public function __construct (& $err)
{
$this->files_arr = Array ();
$this->err = $err;
}
Public Function init ()
{
Run actions only if IMG array sessions var is empty
Check if image directory exists
if (! $this->dir_exists ())
{
Return ' Error retrieving images, missing directory ';
}
Return ";
}
Public Function Get_images ()
{
Run actions only if IMG array sessions var is empty
if (Isset ($_session[' Imgarr '))
{
$this->files_arr = $_session[' Imgarr '];
}
Else
{
if ($dh = Opendir (Imgdir))
{
while (false!== ($file = Readdir ($DH)))
{
if (Preg_match ('/^.*\. jpg|jpeg|gif|png) $/i ', $file)
{
$this->files_arr[] = $file;
}
}
Closedir ($DH);
}
$_session[' Imgarr ' = $this->files_arr;
}
}
Public Function Run ()
{
$curr = 1;
$last = count ($this->files_arr);
if (Isset ($_get[' img '))
{
if (Preg_match ('/^[0-9]+$/', $_get[' img ')) $curr = (int) $_get[' img '];
if ($curr <= 0 | | $curr > $last) $curr = 1;
}
if ($curr <= 1)
{
$prev = $curr;
$next = $curr + 1;
}
else if ($curr >= $last)
{
$prev = $last-1;
$next = $last;
}
Else
{
$prev = $curr-1;
$next = $curr + 1;
}
Line below sets the caption name ...
$caption = Str_replace ('-', ', $this->files_arr[$curr-1]);
$caption = Str_replace (' _ ', ', ', $caption);
$caption = Preg_replace ('/\. jpe?g|gif|png) $/i ', ', $caption);
$caption = Ucfirst ($caption);
Return Array ($this->files_arr[$curr-1], $caption, 1, $prev, $next, $last);
}
Private Function Dir_exists ()
{
return file_exists (Imgdir);
}
}
?>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>Slideshow</title>
<style type= "Text/css" >
body{margin:0;padding:0;font:100% Verdana, Arial, Helvetica, sans-serif;font-size:14px;
div#gallery{border:1px #ccc solid;width:600px;margin:40px Auto;text-align:center;}
Div#gallery img{margin:20px;border:2px #004694 Solid;}
Div#gallery p{color: #004694;}
Div#gallery div.pn{padding:10px;margin:0 5px;border-top:1px #ccc Solid;}
A{color: #333;}
A:hover{color: #cc0000;}
a.sp{padding-right:40px;}
</style>
<body>
<div id= "Gallery" >
<?= $curr;? > "alt=" "/>
<p><?= $caption;? ></p>
<div class= "PN" >
<a href= "? img=<?= $first;? > ">First</a> | <a href= "? img=<?= $prev;? > "class=" sp ">previous</a><a href="? img=<?= $next;? > ">Next</a> | <a href= "? img=<?= $last;? > ">Last</a>
</div>
</div>
</body>

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.