Displaying Randomly Selected Images in PHP

Posted by · October 18, 2013 5:35 am

accepted solutionLearning something new can be both exciting and intimidating. When that something new is as complex as PHP code, the learning curve is high and the intimidation factor is big. Fortunately for PHP beginner, sabregirl, the experts in the PHP topic area on Experts Exchange—specifically Ray_Paseur and GaryG123–were there to walk her through a tough PHP project.

Sabregirl needed to display randomly selected images in PHP. She created both HTML and PHP code, but the two weren’t talking to one another and thus her HTML page was not displaying any images. So, she posted her code, along with a question on Experts Exchange:

“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…?”

Less than 30 minutes after sabregirl asked her question, PHP wizard GaryG123 replied with some suggested code modifications. A little later, PHP Savant and Most Valuable Expert Ray_Paseur chimed in with some sample code and suggested reading material to help sabregirl further educate herself about PHP programming.

“PHP runs on the server.  It prepares the HTML document that is sent to the client.  By the time the client browser sees any data at all, PHP is finished with its work and the server has ‘gone back to sleep.’  If you want a fuller explanation of this, try this article about HTTP client server protocols. It follows that a script that chooses 5 cards at random would run first, then the cards would be displayed in the HTML document after the script has run,” Ray_Paseur said.

Within a few hours, not only did sabregirl have a solution to her problem but also a clearer understanding of both the specific code and how to program in PHP.

This blog post is part of our Featured Solutions series. These posts highlight relevant technology solutions on Experts Exchange and the experts who took the time to provide a quality answer. For exclusive access to over 3 million technology solutions, join the Experts Exchange community for free** today!