Jump to content


Photo

images change


  • Please log in to reply
12 replies to this topic

#1 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 10 September 2006 - 02:14 PM

How do i make an image change on refresh of the page
im stuck without a clue

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 September 2006 - 02:17 PM

That would surely depend on whatever code you're using to generate the html code that displays the image.  If the issue is cached images, then you can add a random number at the end of the image filename to make your browser think it's a different image and therefore fetch it from the server .... <img src="whatever.jpg?asdf1234">
Legend has it that reading the manual never killed anyone.
My site

#3 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 10 September 2006 - 03:40 PM

ok say i have <img src="image1.jpg" /> how can i make it where it changes with other images in a certain folder

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 September 2006 - 03:53 PM

Get the image names into an array and select a random image from the array, then display it. Is there a small fixed number of images, a large fixed number of images, or could the number of images change frequently? Are the images all always in the same folder?

You can use the glob() function to generate an array of specific filetypes from a folder. Examples in the online manual. rand() is also in the manual.
Legend has it that reading the manual never killed anyone.
My site

#5 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 10 September 2006 - 04:53 PM

okay so lets say i have
$image = array("image1.jpg", "image2.gif", "image3.jpg");
echo "<img src=\"$image\" />";

how do i make it choose random

#6 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 10 September 2006 - 05:36 PM

$imgpath = 'path/to/my/crap/pic/';

$dirp = opendir($imgpath); //open the directory
$validimgtypes = array("gif", "jpg", "jpeg", "png", "bmp");
$image = array();

	//read the directory
	while($myfile = readdir($dirp))
	{
	$picfileA = explode(".", $tutorfile); //delimit the file by .
	$picfileA = array_reverse($tutorfileA); //reverse array to get extension
	$ext = strtolower($picfileA[0]); //change ext to lowercase for case-sensitivity
	
		if(in_array($ext, $validimgtypes))
		{
			$image[] = $myfile; 	
		}
	}
	closedir($dirp);


//if there is at least 1 image in the folder
if(count($image) > 1)
{
$randimage = $image[array_rand($image)];
echo "<img src=\"$randimage\" alt=\"$randimage\" />";
}




#7 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 September 2006 - 06:31 PM

okay so lets say i have ... how do i make it choose random


$image = array("image1.jpg", "image2.gif", "image3.jpg");
$the_image = rand(0, count($image)-1);
echo "<img src='". $the_image. "'/>";

Legend has it that reading the manual never killed anyone.
My site

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 10 September 2006 - 07:06 PM

Andy,

$the_image = $image[array_rand($image)];
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 10 September 2006 - 07:51 PM

Andy,

$the_image = $image[array_rand($image)];


Aha! ... live and learn
Legend has it that reading the manual never killed anyone.
My site

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 10 September 2006 - 08:24 PM

Yours was just getting a random index, not the image. (I put it down to cocoa-deficiency ;D)
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 10 September 2006 - 10:57 PM

Hey, I posted that first!

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 10 September 2006 - 11:20 PM

Today, maybe

http://www.phpfreaks...ic,93758.0.html
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 10 September 2006 - 11:25 PM

Of course, in this thread i mean.  :P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users