Jump to content

Matching instances of array_rand help please


Stumboat

Recommended Posts

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!

Link to comment
Share on other sites

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; ?>.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.