help with php (random images script)

  • mista-two-k
  • Beginner
  • Beginner
  • mista-two-k
  • Posts: 57

Post 3+ Months Ago

hello everyone,

i just found a script i needed to display random images on my website. but i still need for each of the random images displayed to have an individual link (e.g. pic one = link one, pic two = link two, etc.)

the script i found does not have this option. however, it seems to be a pretty simple and easy code, and just a little tweaking by adding one or two php-lines would be necessary to make this function.
but...(there are always buts ^_^), i don't know how to do this, so could anyone in here be kind enough to help me out. i'd really appreciate it.

here is the code:

index.php
Code: [ Select ]
<img src='rotate.php?i=0'>image #1
<img src='rotate.php?i=1'>image #2
<img src='rotate.php?i=2'>image #3
  1. <img src='rotate.php?i=0'>image #1
  2. <img src='rotate.php?i=1'>image #2
  3. <img src='rotate.php?i=2'>image #3


rotate.php (the actual code)
Code: [ Select ]
<?php
$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
?>
  1. <?php
  2. $images=array( // list of files to rotate - add as needed
  3.  "bomb.gif",
  4.  "frown.gif",
  5.  "grim.gif",
  6.  "smile.gif" );
  7. $total=count($images);
  8. $secondsFixed=10; // seconds to keep list the same
  9. $seedValue=(int)(time()/$secondsFixed);
  10. srand($seedValue);
  11. for ($i=0;$i<$total;++$i) // shuffle list 'randomly'
  12. {
  13.  $r=rand(0,$total-1);
  14.  $temp =$images[$i];
  15.  $images[$i]=$images[$r];
  16.  $images[$r]=$temp;
  17. }
  18. $index=(int)($_GET['i']); // image index passed in
  19. $i=$index%$total; // make sure index always in bounds
  20. $file=$images[$i];
  21. header("Location: $file"); // and pass file reference back
  22. ?>
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

You can try something like this...
PHP Code: [ Select ]
<?php
/**
* Random image viewer
*/
 
// The images that would be shown.
$images = array(
      'image1.png' => 'http://www.google.com',
      'image2.png' => 'http://www.yahoo.com',
      'image3.png' => 'http://www.www.com',
      'image4.JPG' => 'http://www.www.com'
      );
 
//** You don't have to edit below this line **//
 
// Initiating the $image array
$image = array();
 
// ID that would be provided for each image
$i = 0;
 
// The KEY is the image and the $value is the link
foreach($images as $key => $value)
{
   $image[$i]["LINK"] = $value;
   $image[$i]["IMAGE"] = $key;
   ++$i;
}
 
// Generate a random ID based on number of results.
 
$id = rand(0, $i);
$link = $image[$id]['LINK'];
$image = $image[$id]['IMAGE'];
 
echo <<<EOT
<a href="$link"><img src="$image" alt="$image" /></a>
EOT;
 
?>
  1. <?php
  2. /**
  3. * Random image viewer
  4. */
  5.  
  6. // The images that would be shown.
  7. $images = array(
  8.       'image1.png' => 'http://www.google.com',
  9.       'image2.png' => 'http://www.yahoo.com',
  10.       'image3.png' => 'http://www.www.com',
  11.       'image4.JPG' => 'http://www.www.com'
  12.       );
  13.  
  14. //** You don't have to edit below this line **//
  15.  
  16. // Initiating the $image array
  17. $image = array();
  18.  
  19. // ID that would be provided for each image
  20. $i = 0;
  21.  
  22. // The KEY is the image and the $value is the link
  23. foreach($images as $key => $value)
  24. {
  25.    $image[$i]["LINK"] = $value;
  26.    $image[$i]["IMAGE"] = $key;
  27.    ++$i;
  28. }
  29.  
  30. // Generate a random ID based on number of results.
  31.  
  32. $id = rand(0, $i);
  33. $link = $image[$id]['LINK'];
  34. $image = $image[$id]['IMAGE'];
  35.  
  36. echo <<<EOT
  37. <a href="$link"><img src="$image" alt="$image" /></a>
  38. EOT;
  39.  
  40. ?>
  • mista-two-k
  • Beginner
  • Beginner
  • mista-two-k
  • Posts: 57

Post 3+ Months Ago

hello, thanks for the reply. the problem is that it needs to display 3 random images at a time, and therefore cannot have any duplicates...and btw, i am not too sure how to get ur code to work on my site haha. u'd have to explain me kinda more in details i u dont mind :)

thanks a lot
  • mista-two-k
  • Beginner
  • Beginner
  • mista-two-k
  • Posts: 57

Post 3+ Months Ago

can anyone help me out please?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Why can't you use my script? All you have to change to make it work is the arrays. It goes like this...

'image_url' => 'link_for_image'

that's it...

about three at a time...

You can do it in groups...

Like...
group1 = array(
'img1' => 'link'
'img2' => 'link'
'img3' => 'link'
);
group2 = array(
'img4' => 'link'
'img5' => 'link'
'img6' => 'link'
);
group3 = array(
'img7' => 'link'
'img8' => 'link'
'img9' => 'link'
);
And one would be chosen from each, or if that isn't possible I'll try to come by with an alternative
  • mista-two-k
  • Beginner
  • Beginner
  • mista-two-k
  • Posts: 57

Post 3+ Months Ago

Bogey wrote:
Why can't you use my script? All you have to change to make it work is the arrays. It goes like this...

'image_url' => 'link_for_image'

that's it...

about three at a time...

You can do it in groups...

Like...
$group1 = array(
'img1' => 'link'
'img2' => 'link'
'img3' => 'link'
);
$group2 = array(
'img4' => 'link'
'img5' => 'link'
'img6' => 'link'
);
$group3 = array(
'img7' => 'link'
'img8' => 'link'
'img9' => 'link'
);
And one would be chosen from each, or if that isn't possible I'll try to come by with an alternative



ohh now i get it. so if i want to display 3 random images at a time, i use your "group arrays" and let them display via the function

<img src="$group1" alt="$group1">
<img src="$group2" alt="$group2">
<img src="$group3" alt="$group3">

right?


but still isnt quite what i need...(sorry for being so demanding). basically, i got a website where there are 3 boxes. in each of the box, there should be a random image with an individual link (so far, so good with ur script).
but i want the 3 images to be showing randomly in each of the 3 boxes, and not just a set of images shown in box1, another set in box2 and 3.

what would be possible is using the same set of images in all see "array groups" (so group1, 2 and 3 will all have the same set of images) but there should be no duplicates...

i hope u see what i am trying to say...
and thanks a lot for your time :)
very appriciated
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Alright alright alright :lol: Here you go :P
PHP Code: [ Select ]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
      <title>Image Rotator</title>
 </head>
<body>
<?php
/**
* Random image rotator
*/
 
// Function to generate three different IDs
function get_rands($min, $max, $amount)
{
   $amount = (int) $amount;
   $rands = array();
   $rand = false;
   
   for($i = 0; $i < $amount; ++$i)
   {
      while(!$rand || in_array($rand, $rands))
      {
         $rand = rand($min, $max);
      }
     
      $rands[] = $rand;
   }
   
   return $rands;
}
 
/** The images that would be shown.
*
* Editing Explanation:
* 'source_to_image' => 'website_link'
*/
$images = array(
      'image1.png' => 'http://www.google.com',
      'image2.png' => 'http://www.yahoo.com',
      'image3.png' => 'http://www.www.com',
      'image4.JPG' => 'http://www.ozzu.com'
      );
 
//** You don't have to edit below this line **//
 
// Initiating the $image array
$image = array();
 
// ID that would be provided for each image
$i = 0;
 
// The KEY is the image and the $value is the link
foreach($images as $key => $value)
{
   $image[$i]["LINK"] = $value;
   $image[$i]["IMAGE"] = $key;
   ++$i;
}
 
// Get 3 different IDs
$id = get_rands(0,$i-1,3);
 
// Printing the random IDs for debug... remove or comment when not needed
print_r($id);
 
// Assign IDs to the variables
$id1 = $id[0];
$id2 = $id[1];
$id3 = $id[2];
 
// Defining three different images and links for use
 
// Group 1
$link1 = $image[$id1]['LINK'];
$image1 = $image[$id1]['IMAGE'];
 
// Group 2
$link2 = $image[$id2]['LINK'];
$image2 = $image[$id2]['IMAGE'];
 
// Group 3
$link3 = $image[$id3]['LINK'];
$image3 = $image[$id3]['IMAGE'];
 
// Echoing the HTML...
echo <<<EOT
<p><a href="$link1"><img src="$image1" alt="$image1" /></a><br />
<a href="$link2"><img src="$image2" alt="$image2" /></a><br />
<a href="$link3"><img src="$image3" alt="$image3" /></a></p>
EOT;
 
?>
</body>
</html>
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.  <head>
  4.       <title>Image Rotator</title>
  5.  </head>
  6. <body>
  7. <?php
  8. /**
  9. * Random image rotator
  10. */
  11.  
  12. // Function to generate three different IDs
  13. function get_rands($min, $max, $amount)
  14. {
  15.    $amount = (int) $amount;
  16.    $rands = array();
  17.    $rand = false;
  18.    
  19.    for($i = 0; $i < $amount; ++$i)
  20.    {
  21.       while(!$rand || in_array($rand, $rands))
  22.       {
  23.          $rand = rand($min, $max);
  24.       }
  25.      
  26.       $rands[] = $rand;
  27.    }
  28.    
  29.    return $rands;
  30. }
  31.  
  32. /** The images that would be shown.
  33. *
  34. * Editing Explanation:
  35. * 'source_to_image' => 'website_link'
  36. */
  37. $images = array(
  38.       'image1.png' => 'http://www.google.com',
  39.       'image2.png' => 'http://www.yahoo.com',
  40.       'image3.png' => 'http://www.www.com',
  41.       'image4.JPG' => 'http://www.ozzu.com'
  42.       );
  43.  
  44. //** You don't have to edit below this line **//
  45.  
  46. // Initiating the $image array
  47. $image = array();
  48.  
  49. // ID that would be provided for each image
  50. $i = 0;
  51.  
  52. // The KEY is the image and the $value is the link
  53. foreach($images as $key => $value)
  54. {
  55.    $image[$i]["LINK"] = $value;
  56.    $image[$i]["IMAGE"] = $key;
  57.    ++$i;
  58. }
  59.  
  60. // Get 3 different IDs
  61. $id = get_rands(0,$i-1,3);
  62.  
  63. // Printing the random IDs for debug... remove or comment when not needed
  64. print_r($id);
  65.  
  66. // Assign IDs to the variables
  67. $id1 = $id[0];
  68. $id2 = $id[1];
  69. $id3 = $id[2];
  70.  
  71. // Defining three different images and links for use
  72.  
  73. // Group 1
  74. $link1 = $image[$id1]['LINK'];
  75. $image1 = $image[$id1]['IMAGE'];
  76.  
  77. // Group 2
  78. $link2 = $image[$id2]['LINK'];
  79. $image2 = $image[$id2]['IMAGE'];
  80.  
  81. // Group 3
  82. $link3 = $image[$id3]['LINK'];
  83. $image3 = $image[$id3]['IMAGE'];
  84.  
  85. // Echoing the HTML...
  86. echo <<<EOT
  87. <p><a href="$link1"><img src="$image1" alt="$image1" /></a><br />
  88. <a href="$link2"><img src="$image2" alt="$image2" /></a><br />
  89. <a href="$link3"><img src="$image3" alt="$image3" /></a></p>
  90. EOT;
  91.  
  92. ?>
  93. </body>
  94. </html>

Hope this works :)
  • mista-two-k
  • Beginner
  • Beginner
  • mista-two-k
  • Posts: 57

Post 3+ Months Ago

hey, sorry for the late reply, and thanks a lot for that wonderful script! it's just what i need :)

however (here comes the BUT again ^_^), i somehow cant manage to make it work properly when i use HTML inside the "echo". i need the image to be displayed in 3 boxes which are made from tables with HTML.

here is the HTML code that needs to be echoed
Code: [ Select ]
                     <td width="150" height="136">
                        <img src="IMAGE-1" width="150" height="136" border="0"></a></td>
                        <td width="4" height="136">&nbsp;</td>
                        <td width="150" height="136">
                        <img src="IMAGE-2" width="150" height="136" border="0"></a></td>
                        <td width="4" height="136">&nbsp;</td>
                        <td width="150" height="136">
                        <img src="IMAGE-3" width="150" height="136" border="0"></a></td>
                        <td width="4" height="136">&nbsp;</td>
  1.                      <td width="150" height="136">
  2.                         <img src="IMAGE-1" width="150" height="136" border="0"></a></td>
  3.                         <td width="4" height="136">&nbsp;</td>
  4.                         <td width="150" height="136">
  5.                         <img src="IMAGE-2" width="150" height="136" border="0"></a></td>
  6.                         <td width="4" height="136">&nbsp;</td>
  7.                         <td width="150" height="136">
  8.                         <img src="IMAGE-3" width="150" height="136" border="0"></a></td>
  9.                         <td width="4" height="136">&nbsp;</td>


can u please add your script into this HTML code so it can work...sorry for being always so demanding...and thanks a lot for ur work, very very appreciated!
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Alright, replace the following
Code: [ Select ]
// Echoing the HTML...
echo <<<EOT
<p><a href="$link1"><img src="$image1" alt="$image1" /></a><br />
<a href="$link2"><img src="$image2" alt="$image2" /></a><br />
<a href="$link3"><img src="$image3" alt="$image3" /></a></p>
EOT;
  1. // Echoing the HTML...
  2. echo <<<EOT
  3. <p><a href="$link1"><img src="$image1" alt="$image1" /></a><br />
  4. <a href="$link2"><img src="$image2" alt="$image2" /></a><br />
  5. <a href="$link3"><img src="$image3" alt="$image3" /></a></p>
  6. EOT;

...with this...
Code: [ Select ]
// Echoing the HTML...
echo <<<EOT
                        <td width="150" height="136">
                        <a href="$link1"><img src="$image1" alt="$image1" width="150" height="136" border="0"></a></td>
                        <td width="4" height="136">&nbsp;</td>
                        <td width="150" height="136">
                        <a href="$link2"><img src="$image2" alt="$image2" width="150" height="136" border="0"></a></td>
                        <td width="4" height="136">&nbsp;</td>
                        <td width="150" height="136">
                        <a href="$link3"><img src="$image3" alt="$image3" width="150" height="136" border="0"></a></td>
                        <td width="4" height="136">&nbsp;</td>
EOT;
  1. // Echoing the HTML...
  2. echo <<<EOT
  3.                         <td width="150" height="136">
  4.                         <a href="$link1"><img src="$image1" alt="$image1" width="150" height="136" border="0"></a></td>
  5.                         <td width="4" height="136">&nbsp;</td>
  6.                         <td width="150" height="136">
  7.                         <a href="$link2"><img src="$image2" alt="$image2" width="150" height="136" border="0"></a></td>
  8.                         <td width="4" height="136">&nbsp;</td>
  9.                         <td width="150" height="136">
  10.                         <a href="$link3"><img src="$image3" alt="$image3" width="150" height="136" border="0"></a></td>
  11.                         <td width="4" height="136">&nbsp;</td>
  12. EOT;
  • mista-two-k
  • Beginner
  • Beginner
  • mista-two-k
  • Posts: 57

Post 3+ Months Ago

hey, very nice work u did there!
functions perfectly...but guess what? right, there is a "but" again :P

well this time everything runs smoothly and the script does exactly what i want. but there is always a text above the images like

Array ( [0] => 2 [1] => 7 [2] => 6 )

do u know how to remove it?
thanks a lot for everything man
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

oh lol I forgot to remove my debugging script :oops: so sorry about that. Why didn't you mention that in your previous post? lol my bad. Sorry.

Just remove or comment out the following line:
Code: [ Select ]
print_r($id);

(On line 64)
  • mista-two-k
  • Beginner
  • Beginner
  • mista-two-k
  • Posts: 57

Post 3+ Months Ago

ahhhh, very very nice!! functions perfectly!
thank u very very much :)

but i dont know what i can give you in return..if you need anything, tell me, maybe i can be somewhat of a help.

anyways, i appreciate your time spent into this script and thank you a lot for all the (hard?) work :)

take care man
peace
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

Your welcome :D Any time [that I'm not busy] :D
  • tridevkrishna
  • Born
  • Born
  • tridevkrishna
  • Posts: 1

Post 3+ Months Ago

Hi Bogey,

Sorry to disturb you again!

I have used your script but i am getting following error on line 24($rand = rand($min, $max);)

>> Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\random\index.php on line 24

Can you tell me what can be the problem? I need to use your script so please help me ASAP. I am in hurry schedule right now.

Thanks,
Krishna
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

How many pictures are you trying to process? Are there any other scripts that are running?

Post Information

  • Total Posts in this topic: 15 posts
  • Users browsing this forum: No registered users and 137 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.