We help IT Professionals succeed at work.
Get Started

Display randomly selected images in PHP

3,074 Views
Last Modified: 2013-10-21
Hello Experts,
     I am trying to display 5 random images, each time I click a button.
The following is my HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
	    <meta charset = "UTF-8">
	    <title>Card Changer</title>
	    <?php
	    ini_set( 'display_errors', 1 );
	    error_reporting(E_ALL);
	    ?>
	</head>
	
	<body>
      <h1 align = 'center'>Card Changer</h1>
      <form>
        <table align = 'center'>
          <tr>
            <td><?php print "$array[$num_list[0]]"; ?></td>
            <td><?php print "$array[$num_list[1]]"; ?></td>
            <td><?php print "$array[$num_list[2]]"; ?></td>
            <td><?php print "$array[$num_list[3]]"; ?></td>
            <td><?php print "$array[$num_list[4]]"; ?></td>
          </tr>
          <tr><td align = 'center'><input type = 'button' value = 'Change' onclick = 'document.location = "cardChanger.php"' /></td></tr>
        </table>
      </form>

    </body>
</html>

Open in new window


     And the following is my PHP code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
	    <meta charset="UTF-8">
	    <title>Poker Dealer</title>
	</head>
	
	<body>
      <?php
        ini_set( 'display_errors', 1 );
	    error_reporting(E_ALL);
       $array  = glob ("cards/*.gif"); //store paths in 'cards' directory with '.gif' extension

        $max = count($array); //check how many contents has created in array
  
        //pick five images from "cards" folder, no duplicate, randomly
        $num_list = range(1,$max);
        $num_list = array_rand($num_list,5); 
        shuffle ($num_list);
        
        print "$array[$num_list[0]]"."<br>";
        print "$array[$num_list[1]]"."<br>";
        print "$array[$num_list[2]]"."<br>";
        print "$array[$num_list[3]]"."<br>";
        print "$array[$num_list[4]]"."<br>";
    
      ?>
    </body>
</html>

Open in new window


     As usual, my HTML code is not communicating to my PHP code and my HTML page is not displaying any images now.

     How can I display the multiple images in HTML page using PHP and when I click a button, change the images, without displaying any duplicates?  Is my approach going to a right direction...?

     For this practice, I am using the images of a deck of playing cards and put them into a folder named "cards".  I thought about that writing array for every single card, like "2c.gif", "2d.gif", "2h.gif", "2s.gif", and while I was searching online, I found a piece of code, which might be the way to go...

     Thank you for your help in advance.
Comment
Watch Question
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014
Commented:
This problem has been solved!
Unlock 2 Answers and 17 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE