Stumboat Posted July 22, 2017 Share Posted July 22, 2017 This is the first scripting i've ever done so please forgive my ignorance. I am trying to run a rand script that has matching instances. With the below script, the random picture and random text do not match. I've tried searching how to make this work but I am not even sure what search terms to use. Im aware that I could run the entire html within the array however I would like the data to be easy to edit. This will actually be for a crypto address and associated QR image that has the same name, I would like to be able to quickly upload the QR image and add the address to the array and be ready to go. <?php $pictures = array("train","car","truck","bus","plane",); ?> <img class="alignnone size-full wp-image-322" src="http://localhost/wp/wp-content/uploads/2017/07/<?php echo ''.$pictures[array_rand($pictures)].''; ?>.png" alt="" width="296" height="296" /> This is a picture of a <?php echo ''.$pictures[array_rand($pictures)].''; ?>. Thank you for any help on my little project! Quote Link to comment Share on other sites More sharing options...
kicken Posted July 22, 2017 Share Posted July 22, 2017 Get your random value once and store it into a variable, then echo that variable where you need it. <?php $pictures = array("train","car","truck","bus","plane",); $selectedPicture = $pictures[array_rand($pictures)]; ?> <img class="alignnone size-full wp-image-322" src="http://localhost/wp/wp-content/uploads/2017/07/<?php echo $selectedPicture; ?>.png" alt="" width="296" height="296" /> This is a picture of a <?php echo $selectedPicture; ?>. 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.