解析 xml 导致 html 停止
Parsing xml causes html to stop
这是一个名为 rockwell.php 的 html 文档。 php 脚本在循环时如果 $xmlData->checkerz[0]->attributes()->labro;
有效,但 $xmlData->checkerz[$x]->attributes()->labro;
会阻止 html 的其余部分在循环时加载。为什么这个循环解析会与 html 中它下面的所有内容发生冲突?
<!DOCTYPE html>
<html lang="en">
...
<?php
header('Content-type: application/xml');
$privatecode = 'thisz';
$year = date("Y");
$month = date("n");
$day = date("d");
$url = 'api.php?private='.$privatecode.'&day='.$day.'&month='.$month.'&year='.$year;
$mis = file_get_contents($url);
$xmlData = simplexml_load_string($mis);
if (count($xmlData->checkerz) == 0) {
}
else {
for ($x = 0; $x <= 5; $x++) {
if ($x < count($xmlData->checkerz)) {
$timestampa = $xmlData->checkerz[$x]->attributes()->labro; //stops html here
$timestampb = $xmlData->checkerz[$x]->attributes()->day; //or here
$timestampc = $xmlData->checkerz[$x]->attributes()->month; //or here
$timestampd = $xmlData->checkerz[$x]->attributes()->year; //or here
if ($timestampc == '1') {
$timestampe = 'January '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '2') {
$timestampe = 'February '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '3') {
$timestampe = 'March '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '4') {
$timestampe = 'April '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '5') {
$timestampe = 'May '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '6') {
$timestampe = 'June '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '7') {
$timestampe = 'July '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '8') {
$timestampe = 'August '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '9') {
$timestampe = 'September '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '10') {
$timestampe = 'October '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '11') {
$timestampe = 'November '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '12') {
$timestampe = 'December '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else {
}
}
}
}
?>
...
</html>
因为那是 PHP 的工作方式,除非你做一些事情来分叉这个过程。您的脚本在单个执行线程中执行,该线程一次一步地执行您的代码。由于剩余的 HTML 在循环之后,因此在循环完成之前不会呈现它。
替换这个:
$timestampa = $xmlData->checkerz[$x]->attributes()->labro; //stops html here
$timestampb = $xmlData->checkerz[$x]->attributes()->day; //or here
$timestampc = $xmlData->checkerz[$x]->attributes()->month; //or here
$timestampd = $xmlData->checkerz[$x]->attributes()->year;
这样就解决了:
if ($x == 0) {
$timestampa = $xmlData->checkerz[0]->attributes()-> labro;
$timestampb = $xmlData->checkerz[0]->attributes()->day;
$timestampc = $xmlData->checkerz[0]->attributes()->month;
$timestampd = $xmlData->checkerz[0]->attributes()->year;
}
else if ($x == 1) {
$timestampa = $xmlData->checkerz[1]->attributes()-> labro;
$timestampb = $xmlData->checkerz[1]->attributes()->day;
$timestampc = $xmlData->checkerz[1]->attributes()->month;
$timestampd = $xmlData->checkerz[1]->attributes()->year;
}
else if ($x == 2) {
$timestampa = $xmlData->checkerz[2]->attributes()-> labro;
$timestampb = $xmlData->checkerz[2]->attributes()->day;
$timestampc = $xmlData->checkerz[2]->attributes()->month;
$timestampd = $xmlData->checkerz[2]->attributes()->year;
}
else if ($x == 3) {
$timestampa = $xmlData->checkerz[3]->attributes()-> labro;
$timestampb = $xmlData->checkerz[3]->attributes()->day;
$timestampc = $xmlData->checkerz[3]->attributes()->month;
$timestampd = $xmlData->checkerz[3]->attributes()->year;
}
else if ($x == 4) {
$timestampa = $xmlData->checkerz[4]->attributes()-> labro;
$timestampb = $xmlData->checkerz[4]->attributes()->day;
$timestampc = $xmlData->checkerz[4]->attributes()->month;
$timestampd = $xmlData->checkerz[4]->attributes()->year;
}
这是一个名为 rockwell.php 的 html 文档。 php 脚本在循环时如果 $xmlData->checkerz[0]->attributes()->labro;
有效,但 $xmlData->checkerz[$x]->attributes()->labro;
会阻止 html 的其余部分在循环时加载。为什么这个循环解析会与 html 中它下面的所有内容发生冲突?
<!DOCTYPE html>
<html lang="en">
...
<?php
header('Content-type: application/xml');
$privatecode = 'thisz';
$year = date("Y");
$month = date("n");
$day = date("d");
$url = 'api.php?private='.$privatecode.'&day='.$day.'&month='.$month.'&year='.$year;
$mis = file_get_contents($url);
$xmlData = simplexml_load_string($mis);
if (count($xmlData->checkerz) == 0) {
}
else {
for ($x = 0; $x <= 5; $x++) {
if ($x < count($xmlData->checkerz)) {
$timestampa = $xmlData->checkerz[$x]->attributes()->labro; //stops html here
$timestampb = $xmlData->checkerz[$x]->attributes()->day; //or here
$timestampc = $xmlData->checkerz[$x]->attributes()->month; //or here
$timestampd = $xmlData->checkerz[$x]->attributes()->year; //or here
if ($timestampc == '1') {
$timestampe = 'January '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '2') {
$timestampe = 'February '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '3') {
$timestampe = 'March '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '4') {
$timestampe = 'April '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '5') {
$timestampe = 'May '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '6') {
$timestampe = 'June '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '7') {
$timestampe = 'July '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '8') {
$timestampe = 'August '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '9') {
$timestampe = 'September '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '10') {
$timestampe = 'October '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '11') {
$timestampe = 'November '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else if ($timestampc == '12') {
$timestampe = 'December '.$timestampb.', '.$timestampa.'</br>';
echo $timestampe;
}
else {
}
}
}
}
?>
...
</html>
因为那是 PHP 的工作方式,除非你做一些事情来分叉这个过程。您的脚本在单个执行线程中执行,该线程一次一步地执行您的代码。由于剩余的 HTML 在循环之后,因此在循环完成之前不会呈现它。
替换这个:
$timestampa = $xmlData->checkerz[$x]->attributes()->labro; //stops html here
$timestampb = $xmlData->checkerz[$x]->attributes()->day; //or here
$timestampc = $xmlData->checkerz[$x]->attributes()->month; //or here
$timestampd = $xmlData->checkerz[$x]->attributes()->year;
这样就解决了:
if ($x == 0) {
$timestampa = $xmlData->checkerz[0]->attributes()-> labro;
$timestampb = $xmlData->checkerz[0]->attributes()->day;
$timestampc = $xmlData->checkerz[0]->attributes()->month;
$timestampd = $xmlData->checkerz[0]->attributes()->year;
}
else if ($x == 1) {
$timestampa = $xmlData->checkerz[1]->attributes()-> labro;
$timestampb = $xmlData->checkerz[1]->attributes()->day;
$timestampc = $xmlData->checkerz[1]->attributes()->month;
$timestampd = $xmlData->checkerz[1]->attributes()->year;
}
else if ($x == 2) {
$timestampa = $xmlData->checkerz[2]->attributes()-> labro;
$timestampb = $xmlData->checkerz[2]->attributes()->day;
$timestampc = $xmlData->checkerz[2]->attributes()->month;
$timestampd = $xmlData->checkerz[2]->attributes()->year;
}
else if ($x == 3) {
$timestampa = $xmlData->checkerz[3]->attributes()-> labro;
$timestampb = $xmlData->checkerz[3]->attributes()->day;
$timestampc = $xmlData->checkerz[3]->attributes()->month;
$timestampd = $xmlData->checkerz[3]->attributes()->year;
}
else if ($x == 4) {
$timestampa = $xmlData->checkerz[4]->attributes()-> labro;
$timestampb = $xmlData->checkerz[4]->attributes()->day;
$timestampc = $xmlData->checkerz[4]->attributes()->month;
$timestampd = $xmlData->checkerz[4]->attributes()->year;
}