XML到PHP的字符串处理在浏览器中显示数据表?
我正在用PHP中的explode为大学做一些研究,想尝试将一些XML作为字符串处理,以便在浏览器中显示数据表。我想显示一个HTML表格,带有适当的表格标题,其中每个属性都是它自己的列,每个学生都有一个新行 表中的最后一行应跨越所有列,并包括文本“学生人数:12”(其中计算了学生人数)。我想在字符串上使用XML到PHP的字符串处理在浏览器中显示数据表?,php,html,xml,string,Php,Html,Xml,String,我正在用PHP中的explode为大学做一些研究,想尝试将一些XML作为字符串处理,以便在浏览器中显示数据表。我想显示一个HTML表格,带有适当的表格标题,其中每个属性都是它自己的列,每个学生都有一个新行 表中的最后一行应跨越所有列,并包括文本“学生人数:12”(其中计算了学生人数)。我想在字符串上使用explode,但到目前为止,我只能显示数据-有人能把我推到正确的方向吗 <html> <head><title>Week3</title>
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>
试试这个。这是完美的工作
$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>";
$xml='1!'
12121212
艾伦
汉纳威
社署
1:00
2:30
A.
13131313
彼得
琼斯
PDS
12:00
1:30
B
14141414
克莱尔
墨菲
社署
1:00
2:30
B
杰克
科班
PDS
18181818
雷切尔
哈廷斯
社署
1:00
2:30
约翰
莫洛伊
11:30
A.
20202020
大卫
哈钦森
社署
1:00
2:30
A.
';
$students=explode(“,$xml”);
$stArr=array();
foreach($student作为$student){
preg_match_all(“/(.*?/”,$student,$matches);
$stArr[]=$matches[1];
}
回声“;
回声“;
回音“学生号”
f_名称
s_名称
模块
演讲时间
实验室时间
团体”;
回声“;
回声“;
foreach($stArr作为$arr){
foreach($arr作为$key=>$value){
回显“$value.”;
}
回声“;
}
回声“;
它必须使用explode();你不需要使用explode。使用ajax,您可以获得xml文件的文件数据。这就像php中的file\u get\u contents
。如果你喜欢我的答案,请加分并打分。谢谢,我不懂ajax,我们在大学里从来没有使用过它,我们现在正在使用explode,这就是为什么我想在这里使用它,我明白,但是如果我不知道它是做什么的,它是如何工作的,我就没有意义了。我已经学过了,在讲师说之前我不打算学。他想让我们用explode,你知道jquery吗?如果你知道的话,你会很快理解的
$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>";