I'm trying to display a grid of 9 tiles that display client logos these change randomly to show other client logos in the array (there are 72 client logos). However, I don't want the same logo to ever show twice in the grid of nine. I have tried a thousand different ways to do this but I always end up seeing duplicated logos in the grid.
Can anyone suggest a solution?
This is my code.
` <?php
$clients = $_w->page->get_field('clients');
// Shuffle the clients array randomly
shuffle($clients);
?>
<?php if ($clients): ?>
<section class="image-grid-container">
<script id="hd_client_gallery" type="text/json">
[<?php
$total = count($clients);
$count = 0;
foreach ($clients as $c):
echo "{";
echo '"url": "' . $c->image->url . '",';
echo '"href": "' . $c->link. '"';
echo "}";
if(++$count !== $total) {
echo ", ";
}
endforeach;
?>]
</script>
<?php
$imageCounter = 0;
$usedImages = array();
?>
<ul class="object image-grid gallery">
<?php
// Loop through the $clients array until all logos have been displayed
while ($imageCounter < 9 && count($usedImages) < count($clients)):
// Get a random client from the $clients array
$c = $clients[array_rand($clients)];
// Skip this client if its logo has already been displayed
if (in_array($c->image->url, $usedImages)) {
continue;
}
// Display the logo and mark it as used
$usedImages[] = $c->image->url;
$imageCounter++;
?>
<li>
<a <?= ($c->link ? 'href="'.$c->link.'"' : '' ); ?>>
<img class="img-fluid" src="<?= $c->image->url; ?>" />
<img style="display:none;" class="img-fluid" src="" />
</a>
</li>
<?php endwhile; ?>
</ul>
<?php
// Reset the $usedImages array and shuffle the $clients array again
$usedImages = array();
shuffle($clients);
?>
</section>
<?php endif; ?>`