Jump to content

php random image


barrycorrigan

Recommended Posts

Hi Everyone,

 

I was wondering if anyone could help me make this code I wrote better and a bit more readable and shorter.

 

I have 8 images on the home page so the php code is like this:

 

<?php
$images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
$i = rand(0, count($images)-1);
$selectedImage1 = "_images/showreel/{$images[$i]}.jpg";
?>

<?php
$images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
$i = rand(0, count($images)-1);
$selectedImage2 = "_images/showreel/{$images[$i]}.jpg";
?>

<?php
$images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
$i = rand(0, count($images)-1);
        $selectedImage3 = "_images/showreel/{$images[$i]}.jpg";
?>

<?php
$images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
$i = rand(0, count($images)-1);
$selectedImage4 = "_images/showreel/{$images[$i]}.jpg";
?>

<?php
$images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
$i = rand(0, count($images)-1);
        $selectedImage5 = "_images/showreel/{$images[$i]}.jpg";
?>

<?php
$images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
$i = rand(0, count($images)-1);
$selectedImage6 = "_images/showreel/{$images[$i]}.jpg";
?>

<?php
$images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
$i = rand(0, count($images)-1);
        $selectedImage7 = "_images/showreel/{$images[$i]}.jpg";
?>

<?php
$images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
        $i = rand(0, count($images)-1);
$selectedImage8 = "_images/showreel/{$images[$i]}.jpg";
?>

 

And the HTML is like this:

 

<div id="slider1" class="nivoSlider">
<img src="<?php echo $selectedImage1; ?>" alt="John Richard Cleckheaton West Yorkshire" />
<img src="<?php echo $selectedImage2; ?>" alt="John Richard Cleckheaton West Yorkshire" />
<img src="<?php echo $selectedImage3; ?>" alt="John Richard Cleckheaton West Yorkshire" />
<img src="<?php echo $selectedImage4; ?>" alt="John Richard Cleckheaton West Yorkshire" />
<img src="<?php echo $selectedImage5; ?>" alt="John Richard Cleckheaton West Yorkshire" />
<img src="<?php echo $selectedImage6; ?>" alt="John Richard Cleckheaton West Yorkshire" />
<img src="<?php echo $selectedImage7; ?>" alt="John Richard Cleckheaton West Yorkshire" />
<img src="<?php echo $selectedImage8; ?>" alt="John Richard Cleckheaton West Yorkshire" />
</div>

 

I no this code is terrible but I am still learning. Just a few months ago I had never touched PHP. Any help would be greatly appreciated.

 

Regards

 

Barry

Link to comment
https://forums.phpfreaks.com/topic/234322-php-random-image/
Share on other sites

The for loop is your friend, Barry.

 

<div id="slider1" class="nivoSlider">
<?
   $images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
   $upperBound = count($images) - 1;

   for ($i = 1; $i <= 8; $i++) {
      $rand = rand(0, $upperBound);
?>
      <img src="_images/showreel/<?=$images[$rand]?>.jpg" alt="John Richard Cleckheaton West Yorkshire" />
<?
   }
?>
</div>

Link to comment
https://forums.phpfreaks.com/topic/234322-php-random-image/#findComment-1204492
Share on other sites

  Quote

The for loop is your friend, Barry.

 

<div id="slider1" class="nivoSlider">
<?
   $images = array('image1', '8', 'image2', 'image7', 'image5', 'image4', '9', 'image6');
   $upperBound = count($images) - 1;

   for ($i = 1; $i <= 8; $i++) {
      $rand = rand(0, $upperBound);
?>
      <img src="_images/showreel/<?=$images[$rand]?>.jpg" alt="John Richard Cleckheaton West Yorkshire" />
<?
   }
?>
</div>

for loops may be his friend - but short tags won't be :P keep using <?php not <?

Link to comment
https://forums.phpfreaks.com/topic/234322-php-random-image/#findComment-1204496
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.