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://ift.tt/1pVdo9X" 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://ift.tt/1MvO4Ml" 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://ift.tt/1pVdq1u" 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://ift.tt/1pVdq1u" 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://ift.tt/1MvO6UG" 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.
Any ideas? Any better way of doing this?
Aucun commentaire:
Enregistrer un commentaire