Page 1 of 1
Making an image slideshow with PHP
Posted: Mon Jun 22, 2009 7:01 pm
by lauthiamkok
Hi,
I have a list of images and i want to loop them with a next and previous button. But i cannot get my head around coding it. please let me know if u have any ideas.
Code: Select all
<?php
$arr_img = array(
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg');
$total = count($arr_img);
?>
<div><a href="index.php?previous=<?php echo $previous;?>">Previous</a></div>
<div><a href="index.php?next=<?php echo $next;?>">Next</a></div>
<div><img src="<?php echo $image;?>" /></div>
the images must be looped to go back to the beginning (1.jpg) when it reaches the end of the array, which is 4.jpg.
Many thanks,
Lau
Re: Making an image slideshow with PHP
Posted: Mon Jun 22, 2009 10:27 pm
by Eric!
I think this should work, there might be a bug some where and you need to set paths to the images and I'm assuming the php file is index.php
Code: Select all
<?php
$arr_img = array(
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg');
$total = count($arr_img);
if(!isset($curr)) // setup initial state
{
$curr=0;
}
switch (@$_GET['do'])
{
case "previous":
$curr--;
if($curr<0) $curr=$total;
break;
case "next":
$curr++;
if($curr>$total) $curr=0;
break;
}
?>
<div><a href="index.php?do=previous">Previous</a></div>
<div><a href="index.php?do=next">Next</a></div>
<div><img src="http://yourdomain.com/<?php echo $arr_img[$curr];?>" /></div>
Re: Making an image slideshow with PHP
Posted: Mon Jun 22, 2009 10:54 pm
by McInfo
Code: Select all
<?php
// An array of items
$items = array('A', 'B', 'C', 'D', 'E');
// Hyperlink factory
function link ($i)
{
printf('<a href="?i=%1$s">%1$s</a>', $i);
}
// Shows a list of items and their reference numbers
echo '<ul>';
foreach ($items as $i => $item) {
echo '<li>'.$item.': ';
link($i + 1);
echo '</li>';
}
echo '</ul>';
$first = 1;
$last = count($items);
$current = $first;
// If an index is requested and the request is a number
if (isset($_GET['i']) && !preg_match('/[^0-9]/', $_GET['i'])) {
// If the request is too low...
if ($_GET['i'] < $first) {
// ...loops around to the end
$current = $last;
// If the request is too high...
} else if ($_GET['i'] > $last) {
// ...loops around to the beginning
$current = $first;
// Otherwise...
} else {
// ...uses the requested index
$current = $_GET['i'];
}
}
$previous = $current - 1;
$next = $current + 1;
?>
<ul>
<li>First: <?php link($first); ?></li>
<li>Previous: <?php link($previous); ?></li>
<li>Current: <?php link($current);
// The current item's index is one less than $current
echo ' ('.$items[$current - 1].')'; ?></li>
<li>Next: <?php link($next); ?></li>
<li>Last: <?php link($last); ?></li>
</ul>
Edit: Oops, looks like Eric! beat me too it. But, Eric!, your solution doesn't work because $curr is not passed between pages.
My solution has the advantage of being bookmark-able.
Edit: This post was recovered from search engine cache.
Re: Making an image slideshow with PHP
Posted: Tue Jun 23, 2009 5:40 am
by lauthiamkok
McInfo wrote:Code: Select all
<?php
// An array of items
$items = array('A', 'B', 'C', 'D', 'E');
// Hyperlink factory
function link ($i)
{
printf('<a href="?i=%1$s">%1$s</a>', $i);
}
// Shows a list of items and their reference numbers
echo '<ul>';
foreach ($items as $i => $item) {
echo '<li>'.$item.': ';
link($i + 1);
echo '</li>';
}
echo '</ul>';
$first = 1;
$last = count($items);
$current = $first;
// If an index is requested and the request is a number
if (isset($_GET['i']) && !preg_match('/[^0-9]/', $_GET['i'])) {
// If the request is too low...
if ($_GET['i'] < $first) {
// ...loops around to the end
$current = $last;
// If the request is too high...
} else if ($_GET['i'] > $last) {
// ...loops around to the beginning
$current = $first;
// Otherwise...
} else {
// ...uses the requested index
$current = $_GET['i'];
}
}
$previous = $current - 1;
$next = $current + 1;
?>
<ul>
<li>First: <?php link($first); ?></li>
<li>Previous: <?php link($previous); ?></li>
<li>Current: <?php link($current);
// The current item's index is one less than $current
echo ' ('.$items[$current - 1].')'; ?></li>
<li>Next: <?php link($next); ?></li>
<li>Last: <?php link($last); ?></li>
</ul>
Edit: Oops, looks like Eric! beat me too it. But, Eric!, your solution doesn't work because $curr is not passed between pages.
My solution has the advantage of being bookmark-able.
thanks for this!

Re: Making an image slideshow with PHP
Posted: Tue Jun 23, 2009 12:39 pm
by Eric!
Yeah, I see that now. How about this. You should be able to bookmark this too. I was trying to teach him something by using his own code...
Code: Select all
<?php
$arr_img = array(
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg');
$total = count($arr_img);
if(!isset($_GET['curr'])) // setup initial state
{
$curr=0;
} else $curr=$_GET['curr']; // next image
switch (@$_GET['do'])
{
case "previous":
$curr--;
if($curr<0) $curr=$total;
break;
case "next":
$curr++;
if($curr>$total) $curr=0;
break;
}
?>
<div><a href="index.php?do=previous&curr=$curr">Previous</a></div>
<div><a href="index.php?do=next&curr=$curr">Next</a></div>
<div><img src="http://yourdomain.com/<?php echo $arr_img[$curr];?>" /></div>
Re: Making an image slideshow with PHP
Posted: Tue Jun 23, 2009 3:03 pm
by McInfo
Eric! wrote:Yeah, I see that now. How about this.
Code: Select all
<div><a href="index.php?do=previous&curr=$curr">Previous</a></div>
<div><a href="index.php?do=next&curr=$curr">Next</a></div>
Test your code before you post.
Edit: This post was recovered from search engine cache.
Re: Making an image slideshow with PHP
Posted: Tue Jun 23, 2009 3:56 pm
by Eric!
I can't from this machine to my annoyance. Sorry I forgot the echo. I did spot another thing, count returns the total element number, so you need to subtract one to move it back into the range of the index.
I still think it is a pretty simple solution, even if it didn't compile right in my head and people can't cut and paste it instantly.
(I did say there might be a bug or two...)
Code: Select all
<?php
$arr_img = array(
'1.JPG',
'2.JPG',
'3.JPG',
'4.JPG');
$total = count($arr_img)-1;
if(!isset($_GET['curr'])) // setup initial state
{
$curr=0;
} else $curr=$_GET['curr']; // next image
switch (@$_GET['do'])
{
case "previous":
$curr--;
if($curr<0) $curr=$total;
break;
case "next":
$curr++;
if($curr>$total) $curr=0;
echo "NEXT HIT<br>";
break;
}
?>
<div><a href="junk2.php?do=previous&curr=<?php echo $curr;?>">Previous</a></div>
<div><a href="junk2.php?do=next&curr=<?php echo $curr;?>">Next</a></div>
<div><img src="http://www.sailsarana.com/SRL/<?php echo $arr_img[$curr];?>" /></div>