I'm doing some study for college using explode in PHP and wanted to try to process some XML as a string to display a table of the data in the browser. I want to display a HTML table, with appropriate table headings, where each attribute is its own column, and there is a new row for each student.
The last row in the table should span all columns, and include the text ‘Number of students: 12‘ (where the number is calculated). I was thinking of using explode on the string but so far I can only display the data - can someone push me in the right direction please?
<html>
<head><title>Week3</title></head>
<body>
<?php
$xml = '<students>
<student>
<student_number>12121212</student_number>
<f_name>Alan</f_name>
<s_name>Hannaway</s_name>
<module>SWD</module>
<lecturetime>1:00</lecturetime>
<lab_time>2:30</lab_time>
<group>A</group>
</student>
<student>
<student_number>13131313</student_number>
<f_name>Peter</f_name>
<s_name>Jones</s_name>
<module>PDS</module>
<lecturetime>12:00</lecturetime>
<lab_time>1:30</lab_time>
<group>B</group>
</student>
<student>
<student_number>14141414</student_number>
<f_name>Clare</f_name>
<s_name>Murphy</s_name>
<module>SWD</module>
<lecturetime>1:00</lecturetime>
<lab_time>2:30</lab_time>
<group>B</group>
</student>
<student>
<student_number></student_number>
<f_name>Jack</f_name>
<s_name>Cobane</s_name>
<module>PDS</module>
<lecturetime></lecturetime>
<lab_time></lab_time>
<group></group>
</student>
<student>
<student_number>18181818</student_number>
<f_name>Rachel</f_name>
<s_name>Hartings</s_name>
<module>SWD</module>
<lecturetime>1:00</lecturetime>
<lab_time>2:30</lab_time>
<group></group>
</student>
<student>
<student_number></student_number>
<f_name>John</f_name>
<s_name>Molloy</s_name>
<module></module>
<lecturetime></lecturetime>
<lab_time>11:30</lab_time>
<group>A</group>
</student>
<student>
<student_number>20202020</student_number>
<f_name>David</f_name>
<s_name>Hutchinson</s_name>
<module>SWD</module>
<lecturetime>1:00</lecturetime>
<lab_time>2:30</lab_time>
<group>A</group>
</student>
</students>';
$students = explode("<student>", $xml);
echo '<students>';
foreach($students as $student){
echo '<tr>';
if($students != "<students>"){
$studentNamesOnly = explode("</student>" , $student);
echo '<br>';
}
foreach($studentNamesOnly as $studentName){
echo '<td>';
echo $studentName;
echo '</td>';
}
echo '</tr>';
}
echo '</students>';
?>
</body>
</html>
This is the output I have so far:
12121212 Alan Hannaway SWD 1:00 2:30 A
13131313 Peter Jones PDS 12:00 1:30 B
14141414 Clare Murphy SWD 1:00 2:30 B
18181818 Rachel Hartings SWD 1:00 2:30
20202020 David Hutchinson SWD 1:00 2:30 A