insérer des ibnformations dans un document xml

Mammouth du PHP | 536 Messages

09 juil. 2007, 15:59

Bonjour tout le monde, je vous explique mon problème,

j'ai un formulaire qui contient différents éléments à ajouter dans des fichiers xl et en particuliers des critères.
<form name="form" method="post" action="" id="form">
<input type="hidden" name="form" value="add-practice" />
<input type="hidden" name="section" value="<?php echo $_GET['section']; ?>" />
<input type="hidden" name="id" value="<?php echo ( isset($_GET['id']) ) ? $_GET['id'] : '-1'; ?>" />

<ul class="catalog">
	<li>
		<label class="form-section"><?php echo FORM_YOUR_INFORMATIONS; ?></label>
	</li>
	<li class="left">
		<label><?php echo FORM_YOUR_NAME; ?></label>
		<input type="text" name="author_name" value="" class="text" onChange="setModified();" />
	</li>
	<li class="left">
		<label><?php echo FORM_YOUR_FIRSTNAME; ?></label>
		<input type="text" name="author_firstname" value="" class="text" onChange="setModified();" />
	</li>
	<li class="left">
		<label><?php echo FORM_YOUR_EMAIL; ?></label>
		<input type="text" name="author_url" value="" class="text" onChange="setModified();" />
	</li>
	<li class="right">&nbsp;</li>
	<li style="clear: both; padding-top: 5px;">
		<label class="form-section"><?php echo INFORMATIONS_ABOUT_PRACTICE; ?></label>
	</li>
	<li>
	  <label><?php echo PRACTICE_NAME; ?></label>
	  <input type="text" name="name_practice" value="" class="text" onChange="setModified();" />
  </li>
  <li>
	  <label><?php echo PRACTICE_DESCRIPTION; ?></label>
	  <input type="text" name="description_practice" value="" class="text" onChange="setModified();" />
  </li>
  <li>
    <label class="form-section"><?php echo PRACTICE_CRITERIA; ?></label>
  </li>
  <li>
    <label><?php echo FORM_CHOOSE_CRITERIA; ?></label>
    <?php
        /*---------------------------------*/
        //on ouvre le fichier criteria.xml
        $criteriaFile = '../'.$cfg['ressources_root'].'/criteria.xml';
        $domCriteria = new DOMDocument('1.0','UTF-8');
        $domCriteria->load($criteriaFile);
        $xpCriteria = new DOMXPath($domCriteria);
        $cat_criteria = $domCriteria->getElementsByTagName('cat');
        $result_find_criteria = $domCriteria->getElementsByTagName('criteria');
        
        
        echo "<table align='center' width='100%'><tr>";
        foreach($cat_criteria as $cat){
          $name_cat = $cat->getAttribute('name');
          $id_cat = $cat->getAttribute('id');
          echo "<td valign='top'><br /><strong>".$name_cat." : </strong>";
          foreach ($cat->childNodes as $criteria)
          {
            if ($criteria->nodeType != XML_ELEMENT_NODE) continue;
            if ($criteria->tagName == 'criteria')
            {
              $name_criteria = $criteria->getAttribute('name');
              $id_criteria = $criteria->getAttribute('id');      
              echo "<ul><input type='checkbox' id='crit[]' name='crit[]' value='$id_cat/$id_criteria'>$name_criteria</input></ul>";
            }
          }
        }
        echo "</tr></table>";
    ?>
</ul>

<div class="btns">
	<input type="submit" value="<?php echo SAVE; ?>" class="submit"/>
	<input type="button" value="<?php echo CLOSE; ?>" onclick="formClose()" />
</div>

</form>
lorsqu'on soumet ce formulaire, on est dirigé vers la page qui fait les "enregistrements" dans les fichiers xml.
Voici le code :
// gestion du fichier de liste
if ( $_POST['id'] == '-1' ) {
	$practices = $dsDir.'/'.$_POST['section'].'.xml';
	$document = new DOMDocument('1.0','UTF-8');
	if ( is_file($practices) ) {
		$document->load($practices);
		$root = $document->firstChild;
	}
	else {
		$root = domEl('root');
		$document->appendChild($root);
	}
	$dsId = ( $root->getAttribute('last_insert') != '' ) ? $root->getAttribute('last_insert') + 1 : '1';
	$practice = domEl('practice','',Array('id'=>$dsId,'name'=>$_POST['name_practice'],'viewed'=>'0','used'=>'0', 'subjected'=>'0', 'validated'=>'0', 'subjected_press'=>'0', 'status'=>'0'));
	$root->appendChild($practice);
	$root->setAttribute('last_insert',$dsId);
	$document->save($practices);
	tidyXml($practices);
	$_SESSION['name_practice'] = $_POST['name_practice'];
	$_SESSION['description_practice'] = $_POST['description_practice'];
}
else {
	$dsId = $_POST['id'];
}

//gestion du fichier des critères
$link_crit_pract = $dsDir.'/matrix/criteria-practice.xml';
	$document = new DOMDocument('1.0','UTF-8');
	if ( is_file($link_crit_pract) ) {
		$document->load($link_crit_pract);
		$root = $document->firstChild;
	}
	else {
		$root = domEl('root');
		$document->appendChild($root);
	}	
	$value_post = $_POST['crit'];
	$explode = explode('/', $value_post);
	$id_cat = $explode[0];
	$id_crit = $explode[1];
	$linkId = ( $root->getAttribute('last_insert') != '' ) ? $root->getAttribute('last_insert') + 1 : '1';
	//echo $value_post;
	$link = domEl('link','',Array('id'=>$linkId));
	$root->appendChild($link);
	$root->setAttribute('last_insert',$linkId);
	   $criteria = domEl('criteria','', Array('idcat'=>$id_cat, 'idcrit'=>$id_crit));
	   $link->appendChild($criteria);
	   $practice = domEl('practice', '', Array('id'=>$dsId));
	   $link->appendChild($practice);	   
	$document->save($link_crit_pract);
	tidyXml($link_crit_pract);
L'insertion dans le fichier des critères ne s'effectue pas correctement. En effet, si l'on sélectionne plusieurs critères, il n'y a qu'un seul enregistrement. J'ai alors pensé à une boucle for ou foreach mais je ne vois pas du tout comment faire.

Si quelqu'un pouvait m'aider.

[EDIT]
la solution était toute bête en fait, il suffisait de faire ainsi dans le fichier qui insère dans les fichiers xml :
utiliser un foreach
<?
//gestion du fichier des critères
foreach ($_POST['crit'] as $crit)
{
$link_crit_pract = $dsDir.'/matrix/criteria-practice.xml';
	$document = new DOMDocument('1.0','UTF-8');
	if ( is_file($link_crit_pract) ) {
		$document->load($link_crit_pract);
		$root = $document->firstChild;
	}
	else {
		$root = domEl('root');
		$document->appendChild($root);
	}	
	$value_post = $crit;
	$explode = explode('/', $value_post);
	$id_cat = $explode[0];
	$id_crit = $explode[1];
	$linkId = ( $root->getAttribute('last_insert') != '' ) ? $root->getAttribute('last_insert') + 1 : '1';
	//echo $value_post;
	$link = domEl('link','',Array('id'=>$linkId));
	$root->appendChild($link);
	$root->setAttribute('last_insert',$linkId);
	   $criteria = domEl('criteria','', Array('idcat'=>$id_cat, 'idcrit'=>$id_crit));
	   $link->appendChild($criteria);
	   $practice = domEl('practice', '', Array('id'=>$dsId));
	   $link->appendChild($practice);	   
	$document->save($link_crit_pract);
	tidyXml($link_crit_pract);
}
Un prof désespéré à son élève :
- Et maintenant, dessinez-moi un cercle au tableau... Voila... Alors qu'est-ce que c'est?
- Ben un cercle ?
- Non, c'est votre note, sortez !!