Search the Community
Showing results for tags 'array random number'.
-
The objective is to add a random number to the end of rotating images in a PHP array to prevent browsers from caching the image. The number should be appended to the end of the image URL I have the array, and it works great. I have a random number generator, and that works. I'm doing something wrong when using PHP to apply a random number to the links in the array. Here is the array sample which contains link and image: <?php $content1 = '<a href="http://example.net" title="Example 1" target="_blank"><img src="http://example.com/gallery/image-1.png" alt="Image 1" width="300" height="250" class="gallery-image"></a>'; $content2 = '<a href="http://example.net" title="Example 2" target="_blank"><img src="http://example.com/gallery/image-2.png" alt="Image 2" width="300" height="250" class="gallery-image"></a>'; $content3 = '<a href="http://example.net" title="Example 3" target="_blank"><img src="http://example.com/gallery/image-3.png" alt="Image 3" width="300" height="250" class="gallery-image"></a>'; $content4 = '<a href="http://example.net" title="Example 4" target="_blank"><img src="http://example.com/gallery/image-3.png" alt="Image 4" width="300" height="250" class="gallery-image"></a>'; $content = array($content1, $content2, $content3, $content4,); shuffle($content); ?> <?php print $content[0] ?> This is the random number generator: <?php echo rand() . "\n"; ?> What I want should look like this: $content1 = '<a href="http://example.net" title="Example 1" target="_blank"><img src="http://example.com/gallery/image-1.png?29384756" alt="Image 1" width="300" height="250" class="gallery-image"></a>'; I've attempted to put the random number generator in the array's text string, but I'm doing something wrong, as either it won't generate a number, or the PHP code displays within the HTML, so I'm unsure of how the random number would generate within the array HTML text. Again, the objective is to add a random number query string to the end of the image URL so the array still displays the image and prevents a browser from caching the image when/if a page is refreshed. I'm not sure how or if the random number generator can be placed into the array's text and image URL. Any better way of doing this?