Jump to content

Recommended Posts

Hi, I'm trying to make a simple slideshow with forward and back buttons that change the image inside a div in php. When I press the "further" button, it jumps from index[0] to index[1] and never shows the first image. Then when I click more it doesn't go forward. When I click back, it goes back to a black screen div. Any help getting this to work is GREATLY appreciated because I've been trying for 2 days with tutorials and can't get it. Thanks. Derek

 

Here are the php parts that are relevant, my page was too large and confusing to include.

 

first, the $background array where I store my images.

 

$background = array
(
"<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
"<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>"
);

 

then the code to move the images forward and backwards if the buttons are pressed.

 

if(!empty($_POST['further']))
{
  $currentBackground=next($background);
}
elseif(!empty($_POST['back']))
{
$currentBackground=prev($background); 
} 

 

and now to echo out the images inside the div.

 

<div id="background"><?php echo $currentBackground;?></div>

Like all things php, the $background array doesn't persist across requests. So calling prev() or next() isn't going to work, because upon each request the internal pointer within the $background array will be reset.

 

You would at minimum need to store your images within the $_SESSION array which IS persistent. Really though, this would all be better done using Javascript instead of php.

awesome!! thanks very much. I waited like 2 or 3 days for that. LOL. I don't know javascript, and I looked up "javascript image galleries" and none of them were good for the job, I needed a simple , forward and back image gallery . I altered my code according to what you said but I can't get it to work. Is this right? please.

 

I was unsure of the syntax, so i tried both $_SESSION['background'] and $_SESSION[$background] because I wasn't sure. Neither worked. I got a black screen at the starting point, and the next button worked. But when I start and click back I get a black screen. I have session_start(); at the top of my page.

 

here is the new array

 

$_SESSION['background'] = array
(
"<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
"<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>"
);

 

and here is the POST check.

 

if(!empty($_POST['further']))
{	
     $currentBackground=next($_SESSION['background']);
}
elseif(!empty($_POST['back']))
{
$currentBackground=prev($_SESSION['background']); 
} 

 

and here is the echoing out of the new image.

 

<div id="background"><?php echo $currentBackground;?></div>

 

Please any more help GREATLY appreciated! I'm almost there pleeease.  :D :D :D

You need to call session_start before you can make use of the $_SESSION array.

 

You should also be using isset instead of empty and checking for values, not a lack of.

 

if (isset($_POST['further']))
{   
     $currentBackground=next($_SESSION['background']);
}

if(isset($_POST['back']))
{
   $currentBackground=prev($_SESSION['background']);
}

Great Thorpe thanks again , I changed it, and I had session_start(); at the top of my page before anything else. then next in the page comes this.

 

$_SESSION['background'] = array
(
"<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
"<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>"
);

 

then comes this

 

if(isset($_POST['further']))
{


     $currentBackground=next($_SESSION['background']);
}
elseif(isset($_POST['back']))
{
$currentBackground=prev($_SESSION['background']); 
} 

 

and then the echoing out of the images.

 

<div id="background"><?php echo $currentBackground;?></div>

 

and the first frame wont show up it's black. Because I have a black background.

 

If you know of anything else please let me know. Thanks. I might just have to look for javascript solutions but then again if I can't even do this....lol.

This will keep resetting the array as well.

 

$_SESSION['background'] = array
(
"<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
"<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>"
);

 

Needs to be....

 

if (!isset($_SESSION['background'])) {
  $_SESSION['background'] = array
  (
    "<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
    "<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>"
  );
}

I tried that and it still only goes to the second frame and won't show the first, I created a new file just for this little application to test it and it doesn't work. Sorry about that. Thanks for helping me.

 

Here is the code I used though.

 

<?php
session_start();



if (!isset($_SESSION['background'])) {
  $_SESSION['background'] = array
  (
    "<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
    "<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>",
"<img src='sundragon_environments/ocean/ocean1_FRAME3.jpg'/>",
"<img src='sundragon_environments/ocean/ocean1_FRAME4.jpg'/>",
"<img src='sundragon_environments/ocean/ocean1_FRAME5.jpg'/>"
  );
}

if(isset($_POST['further']))
{
     $currentBackground=next($_SESSION['background']);
}
elseif(isset($_POST['back']))
{
$currentBackground=prev($_SESSION['background']); 
} 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php echo $currentBackground;?>

<form action='NAVIGATION.php' method='post'>
<input type="submit" value="go further" name="further" /> <input type="submit"value="Go back" name="back"/></form>
</body>
</html>

How about this.

 

<?php
session_start();


if (!isset($_SESSION['background'])) {
  $_SESSION['background'] = array
  (
    "<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
    "<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME3.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME4.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME5.jpg'/>"
  );
}

if(isset($_POST['further']))
{
     $currentBackground=next($_SESSION['background']);
}
elseif(isset($_POST['back']))
{
   $currentBackground=prev($_SESSION['background']); 
} 
else
{
  $currentBackground=$_SESSION['background'][0]; 
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php echo $currentBackground;?>

<form action='NAVIGATION.php' method='post'>
<input type="submit" value="go further" name="further" /> <input type="submit"value="Go back" name="back"/></form>
</body>
</html>

hey Dolrich! I still have my Bossgrader and Toptenbox.net haha! Thanks! That code doesn't work either though , it behaves the same way as I described above. Sorry about that, someone in another forum said I might be trying to do something php can't do. I don't know. Here is the code I used though.

 

Thanks. Derek.

 

<?php
session_start();


if (!isset($_SESSION['background'])) {
  $_SESSION['background'] = array
  (
    "<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
    "<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME3.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME4.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME5.jpg'/>"
  );
}

if(isset($_POST['further']))
{
     $currentBackground=next($_SESSION['background']);
}
elseif(isset($_POST['back']))
{
   $currentBackground=prev($_SESSION['background']); 
} 
else
{
  $currentBackground=$_SESSION['background'][0]; 
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php echo $currentBackground;?>

<form action='NAVIGATION.php' method='post'>
<input type="submit" value="go further" name="further" /> <input type="submit"value="Go back" name="back"/></form>
</body>
</html>

Oh, its you. How was your photography site?  :D

 

By the way, try this one. This should work.  8)

<?php
session_start();


if (!isset($_SESSION['background'])) {
  $_SESSION['background'] = array
  (
    "<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
    "<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME3.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME4.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME5.jpg'/>"
  );
}
if(!isset($_SESSION['current_background']))
{
$_SESSION['current_background'] = 0;
}

if(isset($_POST['further']))
{
$_SESSION['current_background'] = isset($_SESSION['background'][$_SESSION['current_background'] + 1]) ? ($_SESSION['current_background'] + 1) : 0;
}
elseif(isset($_POST['back']))
{
   $_SESSION['current_background'] = isset($_SESSION['background'][$_SESSION['current_background'] - 1]) ? ($_SESSION['current_background'] - 1) : count($_SESSION['background']);
} 

$currentBackground=$_SESSION['background'][$_SESSION['current_background']];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php echo $currentBackground;?>

<form action='NAVIGATION.php' method='post'>
<input type="submit" value="go further" name="further" /> <input type="submit"value="Go back" name="back"/></form>
</body>
</html>

Yeah its me Dolrich. You did an AWESOME job coding my sites. Even though neither of them made money. HAHA. Now I'm trying to learn php to make a game. I like php when I'm trying to make games. Other than that I dislike business apps and such. But we'll see. Also, thank you so much for taking the time to code that! I get the following errors though, upon loading it doesn't start at image 1 after I use it, it loops past image 5, and it gives me this error too. If you want to stop you can LOL. I haven't been able to do this for hours and hours. So any more ideas you have or time is GREATLY appreciated. Thanks .Derek.Here is the error:

 

Notice: Undefined offset: 5 in C:\wamp\www\SUN_DRAGON_GAME\NAVIGATION.php on line 35

 

and here is the code as I have it now

 

<?php
session_start();


if (!isset($_SESSION['background'])) {
  $_SESSION['background'] = array
  (
    "<img src='sundragon_environments/ocean/ocean1_FRAME.jpg'/>",
    "<img src='sundragon_environments/ocean/ocean1_FRAME2.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME3.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME4.jpg'/>",
   "<img src='sundragon_environments/ocean/ocean1_FRAME5.jpg'/>"
  );
}
if(!isset($_SESSION['current_background']))
{



$_SESSION['current_background'] = 0;
}

if(isset($_POST['further']))
{



$_SESSION['current_background'] = isset($_SESSION['background'][$_SESSION['current_background'] + 1]) ? ($_SESSION['current_background'] + 1) : 0;
}
elseif(isset($_POST['back']))
{
   $_SESSION['current_background'] = isset($_SESSION['background'][$_SESSION['current_background'] - 1]) ? ($_SESSION['current_background'] - 1) : count($_SESSION['background']);
} 

$currentBackground=$_SESSION['background'][$_SESSION['current_background']];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php echo $currentBackground;?>

<form action='NAVIGATION.php' method='post'>
<input type="submit" value="go further" name="further" /> <input type="submit"value="Go back" name="back"/></form>
</body>
</html>

Hahaha. By the way, heres the fix.

 

Change this code

$_SESSION['current_background'] = isset($_SESSION['background'][$_SESSION['current_background'] - 1]) ? ($_SESSION['current_background'] - 1) : count($_SESSION['background']);

 

To this

$_SESSION['current_background'] = isset($_SESSION['background'][$_SESSION['current_background'] - 1]) ? ($_SESSION['current_background'] - 1) : count($_SESSION['background'])-1;

 

To force it to start with the first image. Use this code

if(( ! isset($_SESSION['current_background'])) OR (! $_POST))
{
    $_SESSION['current_background'] = 0;
}

 

Haha! It got rid of  the error message! AND it starts on the first frame image! LOL Dolrich you're a LOGIC MONSTER!! hehehe. It does loop past the 5th image though, but I won't ask you for more that is a lot. Now I have to go back and understand it all so I learn. I don't use code anymore unless I understand how it works. So I'm guessing it will take me like an hour to figure out that puzzle. I have to start thinking things out but some of the things you did are pretty beyond me, so I'll have to study. Thanks so much! Derek  :D :D :D

If I come up with any more grandiose money making schemes I'll look you up again. Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.