boucler une valeur aléatoire ?

Mammouth du PHP | 601 Messages

18 août 2006, 18:02

Bonjour,
je cherche à boucler un tableau mais j'ai un warning sur mon foreach ?
Mon but est d'afficher une image aléatoire mais rien ne fonctionne ?
Le warning dit que la flèche n'est pas bonne, je comprend plus rien ????

Code : Tout sélectionner

Parse error: parse error, unexpected T_DOUBLE_ARROW in C:\wamp\www\anaska\projet\inc_html\img_aleatoire.php on line 16
<?php
$voiture=array("http://creatif-web.be/anaska/projet/img/blue.jpg", "http://creatif-web.be/anaska/projet/img/carrera.gif", "http://creatif-web.be/anaska/projet/img/carrera1.gif",
"http://creatif-web.be/anaska/projet/img/gray.jpg",
"http://creatif-web.be/anaska/projet/img/red.jpg",
"http://creatif-web.be/anaska/projet/img/red1.jpg");
$contenu=count($voiture);
$alea=mt_rand(1, 6);
echo $alea;

	foreach($voiture => $key as $val) {
echo "$key et $val"; 
	}
//$hasard=array_values($voiture);
//$attt=implode($hasard);
/*foreach($voiture as $val) {
	echo "$val<br />";
	exit(mt_rand());
}*/
//echo "$hasard";
//echo mt_rand($attt,$attt);
?>
Merci
http://xavier-artot.com
¨'°-.,¸¸,.-·²°'´¨'°-.,¸¸,.-·²°'´¨'°-.,¸¸,.-·°'´¨
système d'exploitation "Ubuntu 7.10"

Mammouth du PHP | 1668 Messages

18 août 2006, 18:10

hors mis que $key et $val n'éxiste pas pas, j'aurait plutôt mis :
<?php 
$voiture=array([1] => "http://creatif-web.be/anaska/projet/img/blue.jpg", [2] => "http://creatif-web.be/anaska/projet/img/carrera.gif", [3] => "http://creatif-web.be/anaska/projet/img/carrera1.gif", [4] =>
"http://creatif-web.be/anaska/projet/img/gray.jpg", [5] =>
"http://creatif-web.be/anaska/projet/img/red.jpg", [6] =>
"http://creatif-web.be/anaska/projet/img/red1.jpg"); 
$contenu=count($voiture); 
$alea=mt_rand(1, 6); 
echo $alea; 

?> 
"À ceux qui poursuivent leurs rêves et se spécialisent dans l'impossible" Joseph Kong

10 ans de PHP, déjà.

"moi jtrouve que katagoto il déchire!" Nagol

Mammouth du PHP | 601 Messages

18 août 2006, 18:13

je cherche à récupérer le chemin pas la clé, j'avais penser bouclé le tableau, mais na ne fonctionne même pas, commet fais tu un foreach, s'il te plait ?¿?¿?¿?¿?¿?¿?¿?¿
avec le mt_rand sa me renvoie un chiffre quelquonce, il y a array_values je crois pour le tableau mais je n'y arrive pas ?
edit :
en faite l'écriture correcte :
foreach($voiture as $key => $val
http://xavier-artot.com
¨'°-.,¸¸,.-·²°'´¨'°-.,¸¸,.-·²°'´¨'°-.,¸¸,.-·°'´¨
système d'exploitation "Ubuntu 7.10"

Eléphant du PHP | 451 Messages

18 août 2006, 18:25

Bonjour,
Essayes plutôt :
    foreach($voiture as $key => $val) {
(tu as inversé le => et le as )
Jpaul
J'essaye d'aider : parfois je fais des erreurs, on me les corrige et j'apprends un peu plus. Super ce forum :)

Mammouth du PHP | 601 Messages

18 août 2006, 18:31

Oui merci Jean Paul, j'avais éditer.
Pour récupérer une image aléatoire au chargement, j'ai tenter ça :
$voiture=array('http://creatif-web.be/anaska/projet/img/blue.jpg', 'http://creatif-web.be/anaska/projet/img/carrera.gif', 'http://creatif-web.be/anaska/projet/img/carrera1.gif',
'http://creatif-web.be/anaska/projet/img/gray.jpg',
'http://creatif-web.be/anaska/projet/img/red.jpg',
'http://creatif-web.be/anaska/projet/img/red1.jpg');
$contenu=count($voiture);
$alea=mt_rand(1, 6);

	foreach($voiture as $key => $val) {
 	if($val==$alea) {
	echo $val;
	exit(); }
	}
La boucle démarre à 0 donc sa renvoie false si le mt_rand est de 1 à 6 ?
Je suis dans un puit ?
http://xavier-artot.com
¨'°-.,¸¸,.-·²°'´¨'°-.,¸¸,.-·²°'´¨'°-.,¸¸,.-·°'´¨
système d'exploitation "Ubuntu 7.10"

Mammouth du PHP | 601 Messages

18 août 2006, 18:44

	 function image_aleatoire($repertoire) {
//la variable $repertoire est le chemin d'un répertoire du type $repertoire="./chemin/dossier/"
// Extensions de fichiers à lister, séparer par "|"
$filetype="jpg|jpe|jpeg|gif|png";
//Ouverture puis listing du dossier
$diropen=opendir($repertoire);
while ($filesrc=readdir($diropen))
if (eregi("\.($filetype)$",$filesrc)) $filelist[]=$filesrc;
closedir($diropen);
//Concaténation du chemin du répertoire et de l'image choisie
$filealeatoire=$repertoire.$filelist[floor(rand(0,count($filelist)-1))];
//le résultat de la fonction est le chemin
return $filealeatoire ;
}
$repertoire="../../projet/img/";
$image=image_aleatoire($repertoire);
echo "<img src=".$image." />";
http://xavier-artot.com
¨'°-.,¸¸,.-·²°'´¨'°-.,¸¸,.-·²°'´¨'°-.,¸¸,.-·°'´¨
système d'exploitation "Ubuntu 7.10"