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
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
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>

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

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.