Hi everyone,
I've recently started to learn PHP and struggling to get some code working.
I've created an array with a few images in it, which I want to display on screen as a slideshow, with a next and previous button to go through each picture in the array. Although I can get an image to display on screen I can't get the next and previous buttons to take me to the next and previous images and display them on the same screen.
Here's my code:
<?php
$gallery[0] = 'ponies.jpg';
$gallery[1] = 'bird.jpg';
$gallery[2] = 'ant.jpg';
$gallery[3] = 'french.jpg';
$gallery[4] = 'cat.jpg';
$gallery[5] = 'leaves.jpg';
$countpic = 0;
$currentpic = $gallery[$countpic];
if ($countpic == count($gallery))
{
$currentpic = $gallery[0];
}
elseif($countpic == '-1')
{
$currentpic = count($gallery)-1;
}
echo "<div align=\"left\">";
echo "<table width=\"100%\">";
echo "<tr>";
echo "<td align=\"left\"><img src=\"";
echo $currentpic;
echo "\"/></td>";
echo "</tr>";
echo "</table>";
echo "<a href=\"$currentpic[$countpic]-1\">Previous</a>";
echo "|";
echo "<a href=\"$currentpic[$countpic]+1\">Next</a>";
?>
Does anyone know what I'm doing wrong here?
Thanks
Picture slideshow from an array
Moderator: General Moderators
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: Picture slideshow from an array
I don't think you can directly echo an array item like that, probably better to concatenate:
But I think the real problem is that you are (attempting to) create links to the image filename.
Let's say your script is gallery.php. It might be better to create a link to that, passing the desired image number as get data:
www.domain.net/gallery.php?image=4
Do you see the direction I'm going in?
Code: Select all
echo '<a href="' . $array[$index] . '" />';Let's say your script is gallery.php. It might be better to create a link to that, passing the desired image number as get data:
www.domain.net/gallery.php?image=4
Do you see the direction I'm going in?
Re: Picture slideshow from an array
Hi greyhoundcode,greyhoundcode wrote:I don't think you can directly echo an array item like that, probably better to concatenate:
But I think the real problem is that you are (attempting to) create links to the image filename.Code: Select all
echo '<a href="' . $array[$index] . '" />';
Let's say your script is gallery.php. It might be better to create a link to that, passing the desired image number as get data:
www.domain.net/gallery.php?image=4
Do you see the direction I'm going in?
I think I see where you're going with this although I've never used $_GET before so I'm not sure how I might link it. Is there a good tutorial that will help me understand?
Thanks
Re: Picture slideshow from an array
Code: Select all
<?php
$gallery = array('ponies.jpg', 'bird.jpg', 'french.jpg', 'cat.jpg', 'ant.jpg', 'leaves.jpg');
if (!isset($_GET['image'])) //This will set a default value if no page is being looked for
{
$image = "0"; //This sets the default image to the start of the array
}
else
{
$image = $_GET['image']; //Looks up existing value
}
if ($image == count($gallery)) //If it reaches the end of the array
{
$image = "0"; //It will set the image value back to the beginning of the array
}
elseif ($image == "-1") //If we have reached the first pic
{
$image = count($gallery)-1; //It will set the image value to the end of the array
}
echo "<div align=\"left\">";
echo "<table width=\"100%\">";
echo "<tr>";
echo "<td align=\"left\"><img src=\"";
echo $gallery[$image]; //Displays the current image in the array
echo "\"/></td>";
echo "</tr>";
echo "</table>";
echo '<a href="?image='.($image-1).'">Previous</a> '; //Navigates back through the array (gallery)
echo "|";
echo ' <a href="?image='.($image+1).'">Next</a>'; //Navigates forward through the array (gallery)
?>I've heard one can also do it through a form. Any idea how that would work?