我正在使用多个if语句来检查包含div,并根据容器名称输出图像。代码工作正常,直到我添加最后的“else”或将if更改为elseif,我无法弄清楚为什么会发生这种情况。当我尝试添加else或elseif时,整个页面无法加载。知道为什么会这样吗?
<?php
if($viewMore['container_type'] == 'Large IMG' || $viewMore['container_type'] == 'Gallery') {
$photoSql = "SELECT * FROM `cms_uploads` WHERE (`tableName`='site_content' AND `recordNum` = '".$viewMore['num']."' AND `fieldname`= 'large_images') ORDER BY `order`";
$photoResult = $database->query($photoSql);
$photoResultNum = $database->num_rows($photoResult);
$photoArray = array();
while($photoResultRow = $database->fetch_array($photoResult)) {
array_push($photoArray, $photoResultRow);
}
$large = 0; foreach ($photoArray as $photo => $upload): if (++$large == 2) break;
?>
<img class="features" src="<?php echo $upload['urlPath'] ?>">
<?php endforeach ?>
<?php } ?>
<?php
elseif($viewMore['container_type'] == 'Medium IMG') {
$photoSql = "SELECT * FROM `cms_uploads` WHERE (`tableName`='site_content' AND `recordNum` = '".$viewMore['num']."' AND `fieldname`= 'small_images') ORDER BY `order`";
$photoResult = $database->query($photoSql);
$photoResultNum = $database->num_rows($photoResult);
$photoArray = array();
while($photoResultRow = $database->fetch_array($photoResult)) {
array_push($photoArray, $photoResultRow);
}
$medium = 0; foreach ($photoArray as $photo => $upload): if (++$medium == 2) break;
?>
<img class="features" src="<?php echo $upload['urlPath'] ?>">
<?php endforeach; ?>
<?php } ?>
<?php else { ?> SOMETHING HERE <?php } ?>
编辑:
其他说明
我试过包装休息;在括号中,因为我认为计数之后的那篇文章可能会弄乱一些东西。在endforeach之后完全取出计数器或添加半结肠也无济于事。
答案 0 :(得分:2)
每当你关闭PHP块时,请考虑将其放入PHP的echo函数之外的所有文本/ HTML。
这让我震惊的是这部分:
<?php } ?>
<?php else { ?> ...
转化为:
if (...) {
} echo "[whitespace]"; else {
}
这显然会让你的意外阻止意外。
您不应该在关闭if和打开else之间关闭PHP块,即改为执行此操作:
...
} else {
...