Randomly Display Some Images From A Set of Images: PHP


Use of this script:
1. If you have a photo blog with 100 or more images, using this small php script you can show some random images on your homepage, to keep it more dynamic.. This makes up for a good user experience due to constant fresh content(images) each time.
2. If you have 10 products and you want to show atleast 3 products image on your homepage, using this php script you can randomly display the images and yet not bore your audience!

Video Tutorial: Randomly Display Some Images From A Set of Images: PHP



YouTube Link: https://www.youtube.com/watch?v=ajAj6oAkz60 [Watch the Video In Full Screen.]


Source Code: Randomly Display Some Images From A Set of Images: PHP

(image.php)

  1. <?php  
  2. $pic = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg');  
  3. shuffle($pic);  
  4. ?>  
  5. <html>  
  6.  <head>  
  7.   <title>Random Images</title>  
  8.  </head>  
  9.  <body>  
  10.   <ul>  
  11. <?php  
  12.    for$i = 0; $i < 3; $i++)  
  13.       echo "<li style=\"display: inline;\"> 
  14.                          <img src=\"$pic[$i]\" width=\"250\" height=\"250\"> 
  15.                        </li>";  
  16.    
  17. ?>  
  18.  </ul>  
  19.    
  20.  </body>  
  21. </html>  

Copy and paste above code into a plain text editor. Save the file with .php file extension. Make sure to have 5 image files(with .jpg extensions) in the same folder as image.php

random-image


PHP Code Explanation:

Arrays are very important concept in all programming language.

  1. $pic = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg');  

here $pic is an array variable and is used to store the file names of all the images.

shuffle() is a standard PHP function, which shuffles the content of the array variable. i.e., it re-orders or changes the position of the elements of an array.

  1. <?php  
  2. for$i = 0; $i < 3; $i++)  
  3. echo "<li style=\"display: inline;\"> 
  4.                            <img src=\"$pic[$i]\" width=\"250\" height=\"250\"> 
  5.                         </li>";  
  6. ?>  

Usually unordered listing tags of HTML displays elements vertically.

  1. <li style="display: inline;"> </li>  

But using some css style property, we are showing the images horizontally.
i.e., display: inline;

for loop, loops through 0 to 3(in above example). After the execution of the above line of code(for one instance), we get the following HTML output in the browser.

  1. <ul>  
  2.  <li style="display: inline;">  
  3.             <img src="4.jpg" width="250" height="250">  
  4.  </li>  
  5.  <li style="display: inline;">  
  6.            <img src="5.jpg" width="250" height="250">  
  7.  </li>  
  8.  <li style="display: inline;">  
  9.             <img src="1.jpg" width="250" height="250">  
  10.  </li>  
  11. </ul>  

\ (back slash) is used as an escape character, to escape the effect of (double quote).

2 thoughts on “Randomly Display Some Images From A Set of Images: PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *