【发布时间】:2016-03-24 20:57:39
【问题描述】:
我有包含以下列(id、title、image、text)的数据库表。到目前为止,我只有 3 行它们是:
1 Lorem ipsum dolor [Image-Link] [Text is blank here]
2 [title is blank here] [Image-Link] [Text is blank here]
3 Mediocrem voluptaria [Image-Link] detraxit eleifend pr
这是我的代码:
HTML/PHP
<?php
$resultSet = $db->query("SELECT * FROM table");
if ($resultSet->num_rows != 0)
{
while ($rows = $resultSet->fetch_assoc())
{
$id = $rows["id"];
if ($id <= 3)
{
$images = $rows["image"];
$title = $rows["title"];
echo "<div id=main>";
if ($id == 1)
{
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "<div id=secDiv>";
if ($id == 2)
{
echo "<img id=secImg src=$images>";
}
else
{
echo "<img id=thirdImg src=$images>";
echo "<h2>$title</h2>";
}
echo "</div>";
echo "</div>";
}
}
}
?>
CSS
body{
position: relative;
}
#main{
position: relative;
width: 70%;
height: auto;
margin: 0 auto;
}
#mainImg{
position: absolute;
width: 65%;
}
#mainImg img{
width: 100%;
}
#mainTitle{
position: absolute;
width: 100%;
height: 25%;
bottom: 1.5%;
background-color: rgba(100, 0, 0, 0.7);
}
#mainTitle h2{
color: white;
text-align: center;
font-family: sans-serif;
font-size: 150%;
opacity: 1;
}
#secDiv{
position: absolute;
right: 0%;
top: 0%;
width: 30%;
height: auto;
}
#secImg{
width: 45%;
float: left;
}
#thirdImg{
width: 45%;
float: right;
}
#secDiv h2{
clear: both;
font-size: 12px;
}
我遇到的问题是 else 语句中的 h2 标记出于某种原因正在打印第一个标题和第三个标题。即使第一个标题已经在第一个 if 语句中打印出来,它仍然会在 $id 必须至少为 3 时显示。我做错了什么?
【问题讨论】:
-
你在html中的属性应该用引号括起来。
-
@NiekdeGooijer 是否必须将它们封装在引号中?我问的原因是因为它可以在没有它的情况下工作,或者它只是旧网络浏览器的好习惯?
-
是的。它是 xml/html 标准。浏览器会解析它并尝试猜测结果。