barrycorrigan Posted April 21, 2011 Share Posted April 21, 2011 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 Quote Link to comment Share on other sites More sharing options...
silkfire Posted April 21, 2011 Share Posted April 21, 2011 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> Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 21, 2011 Share Posted April 21, 2011 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 keep using <?php not <? Quote Link to comment Share on other sites More sharing options...
silkfire Posted April 21, 2011 Share Posted April 21, 2011 Looks neater. But yeah pro long tags are pretty hard to convince. Quote Link to comment Share on other sites More sharing options...
barrycorrigan Posted April 21, 2011 Author Share Posted April 21, 2011 Thankyou everyone, I must go away and read more on for and for while loops.. Thanks Barry Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.