好像我还没弄清楚开关是如何工作的,因为不可能(对我来说)做对了。
我有这段代码:
foreach ($this->timeofday as $key => $node) {
if (get_class($node) === 'DOMElement') {
if ($node->hasAttribute('class')) {
switch ($node->getAttribute('class')) {
case "cmil_salong":
echo '<h3>'.$node->nodeValue. '</h3>';
break;
case "cmil_time":
echo $node->nodeValue .' ';
break;
case "mv_3d":
echo '<span class="label label-info">3D</span> ';
break;
case "cmil_rs":
echo '<span class="label label-info">Platser ' . $node->nodeValue . '</span> ';
break;
case "mv_txt":
echo '<span class="label label-info">Textad</span><br>';
break;
}
}
}
if (get_class($node) === 'DOMAttr') {
if ($node->nodeName === 'href') { ?>
<a href="<?php echo $node->nodeValue ?>"><button type="button" class="btn btn-success btn-xs">book! <span class="glyphicon glyphicon-new-window"></span></button></a><br>
<?php
}
}
}
这产生了这个:
<h3>FS 4</h3>10:50
<span class="label label-info">Platser 74</span>
<span class="label label-info">3D</span>
<span class="label label-info">Textad</span><br>
<button type="button" class="btn btn-success btn-xs">book! <span class="glyphicon glyphicon-new-window"></span></button>
但这是预期的结果:
优选地
<div>
11:30 <span class="label label-info">3D</span> <span class="label label-info">textad</span> <span class="label label-info">platser 379</span>
<span class="label label-warning"> Du kan inte längre köpa biljetter till den här förestäs;llningen</span>
</div>
或者,取决于if ($node->nodeName === 'href')
是真的:
<div>
11:30 <span class="label label-info">3D</span> <span class="label label-info">textad</span> <span class="label label-info">platser 379</span>
<button type="button" class="btn btn-success btn-xs">book! <span class="glyphicon glyphicon-new-window"></span></button>
</div>
答案 0 :(得分:0)
试试这个:
foreach ($this->timeofday as $key => $node) {
$div = "<div>";
if (get_class($node) === 'DOMElement') {
if ($node->hasAttribute('class')) {
switch ($node->getAttribute('class')) {
case "cmil_salong":
$div .= '<h3>'.$node->nodeValue. '</h3>';
break;
case "cmil_time":
$div .= $node->nodeValue .' ';
break;
case "mv_3d":
$div .= '<span class="label label-info">3D</span> ';
break;
case "cmil_rs":
$div .= '<span class="label label-info">Platser ' . $node->nodeValue . '</span> ';
break;
case "mv_txt":
$div .= '<span class="label label-info">Textad</span><br>';
break;
}
}
}
if (get_class($node) === 'DOMAttr') {
if ($node->nodeName === 'href') {
$div .= '<a href="'.$node->nodeValue.'"><button type="button" class="btn btn-success btn-xs">book! <span class="glyphicon glyphicon-new-window"></span></button></a><br>';
}
}
echo $div .'</div>';
}