【发布时间】:2015-01-15 02:45:25
【问题描述】:
这是在一个名为rockwell.php 的html 文档中。如果循环时$xmlData->checkerz[0]->attributes()->labro;,则 php 脚本有效,但循环时$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>
【问题讨论】:
-
那么它在哪个迭代上失败了?也许您在
$x中存在某种差异,或者可能缺少某个属性。你能用 XML 更新你的问题吗?同时打开dispaly_errors或检查您的日志,让我们知道错误消息是什么。