Merci, très bon code…
Je l’utilise pour des bannières de partenaires comme ceci :
<?php
$nbRand = 5;
$arrayDone = array();
$arrayData = array(
'<a href=# target=_blank><img src=# border=0><br>Bannière 1</a>',
'<a href=# target=_blank><img src=# border=0><br>Bannière 2</a>',
'<a href=# target=_blank><img src=# border=0><br>Bannière 3</a>',
'<a href=# target=_blank><img src=# border=0><br>Bannière 4</a>',
'<a href=# target=_blank><img src=# border=0><br>Bannière 5</a>'
);
$maxRand = count( $arrayData ) - 1;
$tmpRand = 0;
if($nbRand != $maxRand+1) {
// Pour pouvoir afficher moins de liens que le nombre réel, remplacez par :
// if($nbRand >= $maxRand+1)
echo 'Erreur, vous affichez ', $nbRand, ' liens,
alors que la liste en comporte ', $maxRand+1, ' !';
} else {
for( $i = 0; $i < $nbRand ; $i++ ) {
while( in_array( $tmpRand = rand( 0, $maxRand ), $arrayDone ) );
$arrayDone[] = $tmpRand;
echo $arrayData[ $tmpRand ], '<br><br>';
}}
?>
Ca fonctionne très bien !
(J’ai fait une petite amélioration permettant de savoir le nombre nbRand à mettre en cas d’erreur, afin de ne pas tout recompter manuellement… mettre if($nbRand >= $maxRand+1) si vous voulez pouvoir afficher moins de liens que le nombre réel)
Edité le 08/09/2010 à 10:38