Précédent   Forum des professionnels en informatique > PHP > PHP & SGBD > PHP & MySQL
PHP & MySQL Forum d'entraide sur les fonctions MySQL avec PHP. Avant de poster -> FAQ MySQL, Cours MySQL et Sources MySQL. Pour les questions concernant le moteur MySQL plutôt que les fonctions PHP, merci d'utiliser le forum MySQL.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 24/01/2011, 13h02   #1
Invité de passage
 
Inscription : décembre 2010
Messages : 24
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 24
Points : 1
Points : 1
Par défaut boucle for pour POST

bonjour,

j'ai un formulaire avec du javascript permettant d'ajouter autant de ligne que le souhaite l'utilisateur (Année, Championnat, Classement).

Aussi, je me retrouve avec des éléments postés en nombre, je sais les traiter à l'unité mais pas en groupe.

Voici mon code actuellement, je mes les variables dans un tableau pour les traiter ensuite avec une boucle foreach :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
if(!empty($_POST['item-year-1'])) 
{
							$chronological1 = array("year"=>$_POST['item-year-1'],"championship"=>$_POST['item-championship-1'],"classification"=>$_POST['item-classification-1']);
							$chronological_liste=array($chronological1);
						}
 
if(!empty($_POST['item-year-2'])) 
						{
							$chronological2 = array("year"=>$_POST['item-year-2'],"championship"=>$_POST['item-championship-2'],"classification"=>$_POST['item-classification-2']);
							$chronological_liste=array($chronological1,$chronological2);
						}	
 
						if(!empty($_POST['item-year-3'])) 
						{
							$chronological3 = array("year"=>$_POST['item-year-3'],"championship"=>$_POST['item-championship-3'],"classification"=>$_POST['item-classification-3']);
							$chronological_liste=array($chronological1,$chronological2,$chronological3);
						}

Comment puis-je automatiser la création du tableau avec une boucle for, plutôt que de mettre toutes les possibilités, actuellement je vais comme ça jusqu'à 10 ?

Merci d'avance pour votre aide.
pixworld2 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/01/2011, 13h40   #2
Expert Confirmé
 
Avatar de grunk
 
Homme Olivier
Développeur Web
Inscription : août 2003
Messages : 1 837
Détails du profil
Informations personnelles :
Nom : Homme Olivier
Âge : 27
Localisation : France, Côte d'Or (Bourgogne)

Informations professionnelles :
Activité : Développeur Web
Secteur : Industrie

Informations forums :
Inscription : août 2003
Messages : 1 837
Points : 3 318
Points : 3 318
Nommer des input de la manière suivante :
Code :
name="item-classification[]"
Te permettras d'itérer sur $_POST['item-classification']. Par exemple

Code :
1
2
3
$size = count($_POST['item-classification']);
for($i=0;$i<$size;$i++)
    echo $_POST['item-classification'][$i]
grunk est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/01/2011, 14h38   #3
Invité de passage
 
Inscription : décembre 2010
Messages : 24
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 24
Points : 1
Points : 1
j'ai donc mis en place ta solution et ça fonctionne à merveille.

Code :
1
2
3
4
5
6
						$chronological_liste=array();
						echo $size = count($_POST['item-classification']);
						for($i=1;$i<=$size;$i++) {
							$chronological[$i] = array("year"=>$_POST['item-year'][$i],"championship"=>$_POST['item-championship'][$i],"classification"=>$_POST['item-classification'][$i]);
							array_push($chronological_liste,$chronological[$i]);
						}
Merci beaucoup.
pixworld2 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 04h07.


 
 
 
 
Partenaires

Hébergement Web