我正在大学中使用explode在PHP中进行一些研究,并想尝试将一些XML处理为字符串以在浏览器中显示数据表。我想显示一个具有适当表标题的HTML表,其中每个属性都是其自己的列,并且每个学生都有一个新行。
表格的最后一行应涵盖所有列,并包含文字“学生人数:12”(计算人数)。我当时想explode
在字符串上使用,但是到目前为止我只能显示数据-有人可以将我推向正确的方向吗?
<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>
这是我到目前为止的输出:
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
试试这个。这是完美的工作
$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>";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句