2016-02-13 44 views
0

我有一個帶有不同子菜單的菜單,如果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

你說的是選定的行= 0或數據庫中的空字段名稱? –

回答

2

如果您$result是空的 - 你將永遠不會進入

while($row = $result->fetch_assoc()) 

因爲沒有什麼去取。

所以我想你應該檢查返回的行數。如果在數據庫中找到用戶,則數字將爲1,否則爲0。 所以代碼可以是這樣的(我想你使用mylsqi):

$result = $connection->query($sql); 
if (0 < $result->num_rows) { 
    $row = $result->fetch_assoc(); // you can fetch only one result if you're sure that there's only one user; 
    $name = $row['name']; 
    $lastname = $row['lastname'];?> 
    <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 
} else { 
    // no user found:?> 
    <li class='last'><a href=""><span>SubSub1: EMPTY RESULT</span></a></li> 
<?php 
} 

而且是這樣:

</li> 
</ul> 

刪除此</li>因爲你已經在你的if收盤li

+0

謝謝!我不認爲我會意識到這一點。當我回到電腦時會試用它。 – saltcracker

+0

對不起延遲迴復,但確實有效! – saltcracker