PHP snippet Mostrar imagenes en Random

Jan 2008
31
313 views
0

Generar imagenes aleatorias no es tan dificil como suena y pues esto es gracias a la funcion shuffle() de PHP, la cual toma un array y reordena los indices de un array.

Veamos como funciona:

//CREAMOS UN ARRAY
$imagenes=array();

//AGREGAMOS ITEMS AL ARRAY
$imagenes[]='<img src='imagen1.gif' alt='imagen1' title='imagen1' />';
$imagenes[]='<img src='imagen2.gif' alt='imagen2' title='imagen2' />';
$imagenes[]='<img src='imagen3.gif' alt='imagen3' title='imagen3' />';
$imagenes[]='<img src='imagen4.gif' alt='imagen4' title='imagen4' />';
$imagenes[]='<img src='imagen5.gif' alt='imagen5' title='imagen5' />';

//AHORA SIMPLEMENTE INVOCAMOS LA FUNCION shuffle()
shuffle($imagenes);

//Y LISTA LAS IMAGENES YA ESTAN EN UN ORDEN DIFERENTE AL QUE LAS REGISTRAMOS:
//AHORA YA DEPENDE DE TU IMAGINACION QUE HACER CON ELLAS
//PERO BUENO AQUI PONGO EL CODIGO PARA DESPLEGAR LA IMAGEN ASIGNADA ALEATORIAMENTE
echo $imagenes[0];

De nuevo espero les sea de utilidad.

Dudas y reclamaciones: favor de dejar el comentario.

EOT

Si te gusto este post y te sientes dadivoso,Invitame un cafe

Responder

Mas visitados

Deja tu comentario