Jump to content

Need some help


HeavensGirll

Recommended Posts

Obviously I'm new here - and I really don't know much about PHP - so  here I am.

 

I'm trying to put together a website for the company I work for - I've got the design all finished and I downloaded a script I thought would be perfect. I'm trying to shuffle several images on one page. However the script shuffles the photos - but wont shuffle the links that go with the photos. =( Anyone know a way I can make that happen? Here's some of the code. I added the links into the index.php page - but again they stay static and the images rotate. =(

 

 

<!--

copy the below code into any php file!

-->

 

<a href="http://www.p.com"><img src='rotate.php?i=0'>image #1 
<a href="http://www.phone.com"><img src='rotate.php?i=1'>image #2 
<a href="http://www.phe.com"><img src='rotate.php?i=2'>image #3




this snippet below is from a random.php page 

$images=array( // list of files to rotate - add as needed 
"bomb.gif", 
"frown.gif", 
"grim.gif", 
"smile.gif" ); [code]

If someone could pretty please guide me in the right direction I would be so happy. 

Thanks. 
Charlie 

[/code]

Link to comment
Share on other sites

This is simple enough. This is the way I'd do it

 

<?php

$images = array(
'bomb.html' => 'bomb.gif',
'frown.html' => 'frown.gif',
'somePage.html' => 'grim.gif',
'anotherPage.php' => 'smile.gif'
);

# Randomize the array!
key_shuffle( $images );

# Output the first ( random ) element
echo '<a href="'. key( $images ) .'"><img src="'. current( $images ) .'"></a>';

function key_shuffle (&$array) {
/* Auxiliary array to hold the new order */
$aux = array();
/* We work with an array of the keys */
$keys = array_keys($array);
/* We shuffle the keys */
shuffle($keys);
/* We iterate thru' the new order of the keys */
foreach($keys as $key) {
   	/* We insert the key, value pair in its new order */
	$aux[$key] = $array[$key];
	/* We remove the element from the old array to save memory */
	unset($array[$key]);
}
/* The auxiliary array with the new order overwrites the old variable */
$array = $aux;
}

?>

Link to comment
Share on other sites

Here is the whole index.php page

 

<!--
copy the below code into any php file! 
-->

<a href="http://www.p.com"><img src='rotate.php?i=0'>image #1 
<a href="http://www.phone.com"><img src='rotate.php?i=1'>image #2 
<a href="http://www.phe.com"><img src='rotate.php?i=2'>image #3


then this is the rotate php page. 


<?php 
// rotate images randomly but w/o dups on same page - format: 
// <img src='rotate.php?i=0'> - rotate image #0 - use 'i=1' 
// for second, etc 
// (c) 2004 David Pankhurst - use freely, but please leave in my credit 
$images=array( // list of files to rotate - add as needed 
"bomb.gif", 
"frown.gif", 
"grim.gif", 
"smile.gif" ); 
$total=count($images); 
$secondsFixed=10; // seconds to keep list the same 
$seedValue=(int)(time()/$secondsFixed); 
srand($seedValue); 
for ($i=0;$i<$total;++$i) // shuffle list 'randomly' 
{ 
$r=rand(0,$total-1); 
$temp =$images[$i]; 
$images[$i]=$images[$r]; 
$images[$r]=$temp; 
} 
$index=(int)($_GET['i']); // image index passed in 
$i=$index%$total; // make sure index always in bounds 
$file=$images[$i]; 
header("Location: $file"); // and pass file reference back 
?>
[code]

[/code]

Link to comment
Share on other sites

// Creating Arrays [this should be above all your HTML code //

$images=array( // list of files to rotate - add as needed 
"bomb.gif", 
"frown.gif", 
"grim.gif", 
"smile.gif" ); 

$links = array( // list of links to rotate - add as needed
"http://www.p.com",
"http://www.phone.com",
"http://www.phe.com",
"http://www.lol.com );

function randomize()
  srand(time());
  return = (rand()%4)+1; // generates a random number 1-4
}

function display_image($random) { //prints the image with the correct link
  echo "<a href=\"".$links[$random]."\"><img src=\"".$images[$random]."\"></a>";
}


//Where ever you need to display a random image input --

$random = randomize();
display_image($random);

Hope this helped

 

Sorry there was a small mistake which I addressed

Link to comment
Share on other sites

Skittalz

 

You may want to re-vamp it a little... you want to generate a number between 0-3 inclusively ( array keys start at 0 )

 

And rather than use

(rand()%4)+1

 

You can just use

rand( 0, 3 )

 

Or even better

mt_rand( 0, 3 )

Link to comment
Share on other sites

So you guys know this is hard for me to comprehend here lol.

 

You confused me when you said it goes above all the html. Does that mean it goes in the php rotate page? And then I keep the index.php page how it is with this code below?

 

<!--
copy the below code into any php file! 
-->

<img src='rotate.php?i=0'>image #1 
<img src='rotate.php?i=1'>image #2 
<img src='rotate.php?i=2'>image #3

 

then in the rotate php I'm going to put

 


$images=array( // list of files to rotate - add as needed 
"bomb.gif", 
"frown.gif", 
"grim.gif", 
"smile.gif" ); 

$links = array( // list of links to rotate - add as needed
"http://www.p.com",
"http://www.phone.com",
"http://www.phe.com",
"http://www.lol.com );

function randomize()
  srand(time());
  return = (rand()%4)+1; // generates a random number 1-4
}

function display_image($random) { //prints the image with the correct link
  echo "<a href=\"".$links[$random]."\"><img src=\"".$images[$random]."\"></a>";
}


//Where ever you need to display a random image input --

$random = randomize();
display_image($random);

 

between the php code?

 

I'm sorry I'm getting confused.

 

Link to comment
Share on other sites

;D

 

You might have just made my day. One other question though. I want to have 5 images across the screen and unlimited up and down. So I may have 10 rows of 5 across. Would I just make a table and put the part of the code you told me to put where I want each picture to appear in it's own table or is there an easier way?

 

Give me long enough and I just might confuse you!

Link to comment
Share on other sites

This gets bit trickier, cause you have to loop through images, then check to see if you've looped through 5, insert a linebreak if you have, and continue...

 

Give it a shot and post up an attempt, and I'll help you from there :D

Link to comment
Share on other sites

This should create that table

 

 

$COL = 10; // Number of cols
$ROW = 5; // Number of rows

echo "<table>";
for($y = 0; $y < $COL; $y++) {
 echo "<tr>";
 for($x = 0; $x < $ROW; $x++) {
   $random = randomize();
   echo "<td>";
   display_image($random);
   echo "</td>";
 }
 echo "</tr>";
}
echo "</table>";

 

since everyone is displayin their email i might as well to

skittalz_sbrown at hotmail dot com

Link to comment
Share on other sites

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.