First off, you might want to reconsider teh XML you’re creating.
- all XML is lowercase
- Nest your tags.
- you might want to use the same tag for all functions, so <function id=“graph”>, instead of using a different tag for each function
Anyway. First retrieve all functions for all students, and store them in an array with the student id as key
/*Fetch all functions for all students*/
$sql = 'SELECT *, COUNT(`fonction`) as num_fonctions FROM equipes GROUP BY `id_etudiant`, `fonction`';
$query = $this->db->query($sql);
/*Store in an associative array, with the student id as key*/
$student_functions = array();
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row) {
$student_functions[$row['id_etudiant']][] = $row;
}
}
Then, for the branch, you create a loop for a certain student like this (let’s pretend that the ID for that student is available in $id_etudiant)
/*Add all functions for this student*/
$xml->startBranch('projet');
foreach ($student_functions[$id_etudiant] as $function) {
$xml->startBranch('fonction', array('id' => $function['fonction']));
$xml->addNode('num_functions', $function['num_fonctions']);
$xml->endBranch();
}
$xml->endBranch();
This will give you a clean piece of properly nested XML without any repetitions, like this:
<fonction id="Chef de projet">
<num_functions>1</num_functions>
</fonction>
<fonction id="Designer">
<num_functions>2</num_functions>
</fonction>
</projet>
I’m sure you can figure out any modifications yourself.
Hope to have been of help here.
EDIT: alternatively, you could also construct an XML like:
<fonction>
<jobtitle>Chef de projet</jobtitle>
<type>project</type>
<num_functions>1</num_functions>
</fonction>
<fonction>
<jobtitle>Designer</jobtitle>
<type>graph</type>
<num_functions>2</num_functions>
</fonction>
</projet>
But that’s really up to you.