我有数据库表,其中包含以下列(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
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 ""; if ($id == 1) { echo ""; } } } ?>"; echo ""; echo ""; } echo ""; echo ""; echo "$title
"; echo ""; if ($id == 2) { echo ""; } else { echo ""; echo ""; echo "$title
"; } echo "
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.我做错了什么?
如果我们快速检查你的代码
if ($id <= 3) { //Executes for ID: 1, 2 and 3 if ($id == 1) { //Executes for ID: 1 } if ($id == 2) { //Executes for ID: 2 } else { //Executes if ID !== 2 //So this part executes for ID: 1 and 3 } }
如果我理解正确,你希望你的最后一个只为ID 3执行.在这种情况下你需要另一个IF,或者更好,看看PHP的switch语句(http://php.net/manual/en /control-structures.switch.php).