0

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

1 Answer 1

0

try this. it is work perfect

    $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);
$stArr = array();


foreach ($students as $student) {

        preg_match_all("/<.*?>(.*?)<\/.*?>/", $student, $matches);
        $stArr[] = $matches[1];
}


echo "<table border=\"1\" >";
echo "<tr>";
echo "<th>student_number</th>
      <th>f_name</th>
      <th>s_name</th>
      <th>module</th>
      <th>lecturetime</th>
      <th>lab_time</th>
      <th>group</th>";
echo "</tr>";
echo "<tr>";
foreach ($stArr as $arr) {

foreach ($arr as $key => $value) {
     echo "<td>".$value."</td>";
}

echo "</tr>";
}

echo "</table>";
Sign up to request clarification or add additional context in comments.

8 Comments

it has to use explode();
you don't need to use explode. with ajax you get a file data of xml file. this is like file_get_contents with php. if you like my answer please mark up and rate it . thanks
i dont understand ajax and we have never done it in college we are using explode at the moment thats why i want to use it here
i understand that but there is no point me doing it if i dont know what it does how it works. I have studied it and dont plan to till my lecturer says so. He wants us to use explode.
you know jquery? if you know , you understand it very fast
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.