6 Replies - 725 Views - Last Post: 28 October 2009 - 08:31 AM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

"Flipping through" images

Posted 26 October 2009 - 02:34 PM

Hi everybody,
If I have an image embedded in an HTML page with the IMG tag, is there a way to create a PHP script that can change the image to another specific one when a button is clicked? I can't use Javascript, unfortunately, and I'm not sure if this can be done using PHP; can anyone give me some advice on this, please?

Thanks,
Zel2008
Is This A Good Question/Topic? 0
  • +

Replies To: "Flipping through" images

#2 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

Re: "Flipping through" images

Posted 26 October 2009 - 03:49 PM

sorry you cant do this with php unless you want to reload the whole page each time

jave script is your best option but to bad you cant use java script, though what would hold you back from using java script?
Was This Post Helpful? 0
  • +
  • -

#3 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: "Flipping through" images

Posted 26 October 2009 - 03:55 PM

Hmm... well I don't think PHP can dynamically change a page without reloading it. You could make a button to change the picture though, it'd just have to reload the page as well. You could, say, store all the picture locations on the server in an array and then increment through them with the button loading the next picture when the page refreshes.

You could also put the page that has to be loaded over and over inside an IFrame on the page that you don't want to load over and over. Then when the button was clicked, the frame would refresh and load the next picture but the page the frame is on wouldn't.

Either way, here is an example of the PHP way (you can frame this if you want):

Hope it helps you :)

<?php

// Store the locations to all your images here
$images = array("images/test1.png", "images/test2.png", "images/test3.png");

// Grab the index of the picture from the URL
$index = $_GET["index"];

// If there is no index, set it to 0 (the first picture in the array)
if (!$index)
	$index = 0;

// If the index is greater than the number of images, set to 0
if ($index > count($images) - 1)
	$index = 0;

?>

<img src="<?php echo $images[$index]; ?>" /><br />

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="get">
<input type="hidden" name="index" value="<?php echo ($index + 1); ?>" /><input type="submit" value=" Next Picture " />
</form>


Was This Post Helpful? 1
  • +
  • -

#4 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: "Flipping through" images

Posted 27 October 2009 - 05:54 AM

Thanks guys,
I'll have to try out that PHP solution. My boss suggested CGI also, but I've never done something like this with CGI; does anyone know how that would work? I always thought we had to have a completely different program installed for CGI.

EDIT: And Master_Jake, would you mind expanding on that iframe solution? That sounds very interesting, and I've never heard of that element before.
Thanks,
Zel2008

This post has been edited by Zel2008: 27 October 2009 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: "Flipping through" images

Posted 27 October 2009 - 07:33 PM

Sure. By the way, the above PHP code I posted does work. Pop that code into a file, say photogallery.php

Obviously, change the file and image paths in the array to match the file and image paths of your actual images. You can add or remove as many as you want by appending a new item onto the array using , "path/file.png" but I'm sure you knew that.

With the element iFrame, you can embed a webpage into another web page (similar to how frames work, except you can position an iFrame anywhere into a normal page without using regular frames).

For example:

index.html
<html>
<body>

<iframe src="photogallery.php" width="640" height="480">
<span>Browser does not support iframes.</span>
</iframe>

</body>
</html>



The important code here is the iframe tags. The src element is the path to the page you wish to embed. The width and height are self-explanatory. There are plenty other options you can set such as whether or not the frame has a border, whether or not you can scroll in it, etc. Click here to view all of the attributes.

Of course, you put the iframe tag wherever you want the embedded page to appear. That's all.

Pages that are loaded inside the iframe stay inside the iframe. If there are links on that page and a user clicks them, it will load up the linked page inside the iframe.

In this case, it will refresh the photogallery page sending in the new picture index and will load the new image inside the iframe without affecting its parent page.

Hope it helps.
Was This Post Helpful? 0
  • +
  • -

#6 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: "Flipping through" images

Posted 28 October 2009 - 06:57 AM

Thanks for explaining that so thoroughly, I appreciate it. I think I should be fine with this now.
Thanks,
Zel2008
Was This Post Helpful? 0
  • +
  • -

#7 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: "Flipping through" images

Posted 28 October 2009 - 08:31 AM

You're welcome. Glad I was able to assist :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1