Page 1 sur 1

Extraire les données d'un tableau html dans un xml

Posté : 25 févr. 2014, 09:11
par Nathamis
Bonjour !!
voilà mon problème, j'ai crée un tableau en html à deux entrées et j'aimerais pouvoir extraire les donnée dans un fichier xml.
Pour que j'arrive mieux à vous expliquer je vous ai fait une maquette de mon tableau :)

Image

Voilà alors ce que je voudrai faire c'est dire quand dans tel ou tel trip il y a tel ou tel personnes, le xml donnerai ça :
<trip name="Trip1">
     <participants id="0" prenom="Pauline" />
     <participants id="3" prenom="Sophia" />
     <participants id="5" prenom="Pierre" />
</trip>
<trip name="Trip2">
     <participants id="1" prenom="Jean" />
     <participants id="2" prenom="Eric" />
     <participants id="3" prenom="Sophia" />
</trip>
<trip name="Trip3">
     <participants id="0" prenom="Pauline" />
     <participants id="2" prenom="Eric" />
     <participants id="3" prenom="Sophia" />
     <participants id="4" prenom="Nathalie" />
</trip>
Voilà et le html est construit de cette façon :
<table>
	<thead>
		<tr>
			<td></td>
			<td></td>
			<td>Trip1</td>
			<td>Trip2</td>
			<td>Trip3</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>0</td>
			<td>Pauline</td>
			<td><input type="checkbox" name="trip1" value="0" /></td>
			<td><input type="checkbox" name="trip2" value="0" /></td>
			<td><input type="checkbox" name="trip3" value="0" /></td>
		</tr>
		<tr>
			<td>1</td>
			<td>Jean</td>
			<td><input type="checkbox" name="trip1" value="1" /></td>
			<td><input type="checkbox" name="trip2" value="1" /></td>
			<td><input type="checkbox" name="trip3" value="1" /></td>
		</tr>
		<tr>
			<td>2</td>
			<td>Eric</td>
			<td><input type="checkbox" name="trip1" value="2" /></td>
			<td><input type="checkbox" name="trip2" value="2" /></td>
			<td><input type="checkbox" name="trip3" value="2" /></td>
		</tr>
	</tbody>
</table>
et voilà ce que j'ai commencé à faire avec dom php :
for($i=0; $i<100; $i++){
	if (isset($_POST['trip'.$i])){
		$trip = $dom->createElement('trip');
			$particpant= $dom->createElement('participant');
				$idPart = $dom->createAttribute('id');
				$idPart->value = $_POST['trip'.$i];
			$trip->appendChild($idPart);
		$tripList->appendChild($trip);
	}
}
j'ai créer au dessus un noeud Trip list, le truc c'est qu'avec le code que j'ai fait ca va créer un nouveau trip à chaque fois, alors qu'il faudrai créer une boucle pour les trip puis une boucle pour les participants, mais là je bloque un peu ....

Merci beaucoup pour votre aide !!

Re: Extraire les données d'un tableau html dans un xml

Posté : 25 févr. 2014, 10:51
par xTG
Où récupérer les participants ?
Si tu sais répondre à cette question tu peux faire une boucle sur les données du type :
for($i=0; $i<100; $i++){
        if (isset($_POST['trip'.$i])){
                $trip = $dom->createElement('trip');
                foreach($participants as $cle => $valeur)
                {
                        $particpant= $dom->createElement('participant');
                                $idPart = $dom->createAttribute('id');
                                $idPart->value = $_POST['trip'.$i];
                        $trip->appendChild($idPart);
                }
                $tripList->appendChild($trip);
        }
}
Après tu as un léger souci, tu ne fait absolument rien de $participant. ;)

Re: Extraire les données d'un tableau html dans un xml

Posté : 25 févr. 2014, 11:04
par Nathamis
hummmm
j'ai encore un peu de mal avec les foreach c'est possible de m'expliquer vite fait, qu'est que $cle et $valeur sont censé être ?
Les participants sont les case check coché, du coup je doit les mettre dans un tableau ?

Effectivement je me suis emmêlé les pinceau avec les participant, ca devrai donner ça normal :
$participant= $dom->createElement('participant');
     $idPart = $dom->createAttribute('id');
     $idPart->value = $_POST['trip'.$i];
     $participant->appendChild($idPart);
$trip->appendChild($participant);

Re: Extraire les données d'un tableau html dans un xml

Posté : 25 févr. 2014, 14:25
par xTG
Il y a des exemples dans la documentation. :)
http://fr2.php.net/manual/en/control-st ... oreach.php