我有一個帶有不同子菜單的菜單,如果SQL結果爲空或者不是,我想在其中顯示不同的特定子子菜單。SQL結果爲空時的不同子菜單
如果用戶名(會話用戶名)存在於該數據庫中,它將輸出名和姓,因爲$result
不爲空。但是,如果我登錄到該數據庫中沒有用戶名的帳戶,則不會輸出<li class='last'><a href=""><span>SubSub1: EMPTY RESULT</span></a></li>
。
它只是輸出這樣的:
<li class='active has-sub'><a href=""><span>Sub1</span></a>
<ul>
</li>
</ul>
當它應該是:
<li class='active has-sub'><a href=""><span>Sub1</span></a>
<ul>
<li class='last'><a href=""><span>SubSub1: EMPTY RESULT</span></a></li>
</ul>
爲什麼它出來是空的?我究竟做錯了什麼?
<form action="user.php" method="GET">
<div id="cssmenu">
<ul>
<li class='active has-sub'><a href='#'><span><?php echo htmlentities($_SESSION['username'], ENT_QUOTES, 'UTF-8'); ?></span></a>
<ul>
<tr>
<li class='last'><a href=""><span>Menu1</span></a></li>
<li class='last'><a href=""><span>Menu2</span></a></li>
<li class='active has-sub'><a href=""><span>Menu3</span></a>
<ul>
<li class='active has-sub'><a href=""><span>Sub1</span></a>
<ul>
<?php
require('connections/db_connection.php');
$username = $_SESSION['username'];
$sql = "SELECT name, lastname FROM servers";
$sql .= " WHERE username = '$username'";
$result = $connection->query($sql);
while($row = $result->fetch_assoc()){
$name = $row['name'];
$lastname = $row['lastname'];
if (empty($result)){
?>
<li class='last'><a href=""><span>SubSub1: EMPTY RESULT</span></a></li>
<?php
}else{
?>
<li class='last'><a href=""><span>SubSub1: <?php echo $name; ?><br>
Last name: <?php echo $lastname; ?></span></a></li>
<li class='last'><a href=""><span>SubSub2</span></a></li>
<?php
}
}
$connection->close();
?>
</li>
</ul>
<li class='last'><a href=""><span>Sub2</span></a></li>
<li class='last'><a href=""><span>Sub3</span></a></li>
</li>
</ul>
<li class='last'><a href="logout.php"><span>Logout</span></a></li>
</tr>
</ul>
</li>
</ul>
</div>
</form>
你說的是選定的行= 0或數據庫中的空字段名稱? –