Display data from php from xml

I want to get and display some values โ€‹โ€‹from xml code using php. So when I delete the "base" based on the "scores" I can display what I want, but I have to put that ballast on multiple "tables". This is my xml:

<?xml version="1.0" encoding="UTF-8"?>


<base>  
<table nom="analyse">
    <champs>
        <nom>id_analyse</nom>
        <decription>Identifiant de l'analyse</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>

    <champs>
        <nom>id_zp</nom>
        <decription>Identifiant de la zone de prรฉlรจvement</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>

    <champs>
        <nom>id_essai</nom>
        <decription>Identifiant de l'essai</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>

    <champs>
        <nom>id_traitement</nom>
        <decription>Identifiant du traitement</decription>
        <type>Char</type>
        <type_entree>Facultatif</type_entree>
    </champs>

    <champs>
        <nom>code_traitement</nom>
        <decription>Code_traitement</decription>
        <type>Char</type>
        <type_entree>Facultatif</type_entree>
    </champs>
</table>

<table name="bloc">
    <champs>
        <nom>id_bloc</nom>
        <decription>Identifiant du bloc</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>

    <champs>
        <nom>bloc</nom>
        <decription>Nom du bloc</decription>
        <type>Char</type>
        <type_entree>Facultatif</type_entree>
    </champs>

    <champs>
        <nom>id_essai</nom>
        <decription>Identifiant de l'essai</decription>
        <type>Char</type>
        <type_entree>Obligatoire</type_entree>
    </champs>
</table>
</base>

      

And this is my php code:

  <?php
  $fichier = 'arbre_xml_BDD.xml';
  $xml = simplexml_load_file($fichier);

  foreach($xml as $champs){
      echo $champs->nom.'</br>';
      echo $champs->decription.'</br>';
      echo $champs->type.'</br>';
      echo $champs->type_entree.'</br>';
      echo'</br>';

  }
  ?>

      

Help me please!

+3


source to share


1 answer


You need to do another foreach statement, your first one will only go across the table and not to champions, see below:



<?php
$fichier = 'arbre_xml_BDD.xml';
$xml = simplexml_load_file($fichier);

foreach ($xml as $table) { // Loop around each <table> element
    foreach ($table as $champs) { // Loop around each sub child (champs) of <table> parent
        echo $champs->nom.'</br>';
        echo $champs->decription.'</br>';
        echo $champs->type.'</br>';
        echo $champs->type_entree.'</br>';
        echo'</br>';
    }
}
?>

      

+4


source







All Articles