2017-04-21 84 views
1

我有點問題,如果沒有類別我需要回聲沒有類別,如果有我需要回聲有分類。它顯示是否有類別,但不顯示是否沒有。回聲陳述只返回一個

<tr> 
    <?php 

    $db = dbconnect(); 
    $stmt = $db->prepare("SELECT * FROM discussion_categories"); 
    $stmt->execute(); 
    $result = $stmt->get_result(); 

    while (($row = mysqli_fetch_assoc($result)) == true) { 

    $CategoryID = $row['CategoryID'];  
    $Name = $row['Name']; 
    $Description = $row['Description']; 
    $Photo = $row['Photo']; 



    if(!empty($CategoryID['CategoryID'])){ 

     echo "<td>No categories</td>"; 

    } else { 

     echo "<td colspan='4'><img class='profile-photo' src='" . ROOT_URI . "/uploads/" . $Photo . "'></td>"; 
     echo "<td colspan='4'><a href='discussions.php?view={$CategoryID}'>{$Name}</a></td>"; 
     echo "<td><a href='managecategories.php?delete={$CategoryID}'>delete</a></td>"; 
    } 


} 

?>  
    </tr> 
+0

如果'CategoryId'爲** not **空,顯示「No categories」?我很困惑。爲什麼有'$ CategoryID = $ row ['CategoryID']; '?只需使用'$ row' –

+0

當沒有類別時會發生什麼?它是空白的嗎? – FirstOne

+0

你如果語句應該在循環檢查之前開始,如果你有結果,那麼如果沒有迴應沒有 –

回答

3

這是一個容易解決的問題。

如果沒有類別,while循環從未執行,因爲db結果中現在有行。

嘗試,並首先檢查該行量:

if (mysqli_num_rows($result) == 0) { 

    //there are no categories 
    echo('no categories'); 

} else { 

    //there are categories 
    echo('there are categories'); 

    //in case you want to loop through your categories now 
    while (($row = mysqli_fetch_assoc($result)) == true) { 

    $CategoryID = $row['CategoryID']; 
    echo($CategoryID); 
    //your code 
} 

} 

這會幫助你!

+1

小編碼風格點,但'回聲'不是一個函數,所以通常不用括號寫 - 所以'echo $ CategoryID;'不'echo($ CategoryID);'。 – IMSoP

2

如果沒有分類,那麼這個條件:

while (($row = mysqli_fetch_assoc($result)) == true) 

這是寫這個的長篇大論方式:

while ($row = mysqli_fetch_assoc($result)) 

永遠不會爲真,那麼你」會進入循環零次 - 對於$row永遠不會有「真值」值。


如果我們寫出來的代碼爲僞代碼,我們得到:

inspect each result 
    if the result has a non-empty CategoryID, echo "No categories" 
    if the result has an empty CategoryID, echo "There are categories" 
end of loop 

兩個如果檢查周圍走錯了路,但更重要的是他們是循環內。

什麼你大概意思是這樣的:

set found_results flag to false 
inspect each result 
    if the result has a non-empty CategoryID, set found_results flag to true 
    perform other operations on the result, or use "break;" to end the loop early 
end of loop 
if found_results flag is true, echo "There are categories" 
if found_results flag is false, echo "No categories" 

我要把它留給你把這一回PHP。 :)

當然,如果你真的只需要知道,如果有結果還是不行,你可以更整潔通過這樣寫:

  • 計數行使用mysqli_num_rows
  • 使用SELECT COUNT(*)退回了您SQL代替SELECT *,可能還帶有WHERE CategoryId IS NOT NULL條款
+0

儘管不需要添加== true,但它實際上不會產生任何影響。因爲一個double等於true將匹配任何不是的:一個空字符串,0,null,空數組,錯誤 – Augwa

+0

@Augwa是的,我意識到,就像我說的那樣,寫一個同樣的東西是一種冗長的方式。儘管如此,我認爲它在這種情況下會極大地損害可讀性,所以我將其發佈出來。 – IMSoP

+0

@Augwa我需要添加它由Zend Support提供的真實值。因爲沒有它,它會在條件警告標誌中給出Assignment。 – Case

0

如果我理解正確的話,你應該使用空()不是!空()。根據你的代碼

if(!empty($CategoryID['CategoryID'])){ //if cat is not empty it says no cat 
    //notice the ! in front of empty tag 
    echo "<td>No categories</td>"; 
} else { 
    echo "<td>There are categories</td>"; 
} 

,如果類別是空的,它會顯示有三類: 代碼的運行等。

+1

這個錯誤當然存在,但我想,這不是OP詢問的問題。問題說,沒有類別的輸出是*空白*,這是因爲循環從不輸入。 – IMSoP

+0

o亞..我錯過了 – sagar

0

通過調用While循環,您說如果查詢返回結果中至少有一個類別。

當沒有類別時,循環會因此感到困惑。嘗試用大於零的CategoryID的計數替換empty(),看看會發生什麼。

如果> 0

存在

其他

不存在

1

如果你沒有結果,那麼它甚至不會在while循環中進行,所以你的條件語句是多餘的(這就是爲什麼你沒有輸出)。

如果您在嘗試對它們做任何事情之前查看結果的某些註釋中提到過,那麼您最好關閉它。

if($result->num_rows > 0) { 
    // you now know there are results 
    while ($row = mysqli_fetch_assoc($result)) { 
     // do your business in here as you would have, but you dont need to worry about nothing to process 
    } 
} else { 
    // do something in here to send back a null result, or whatever you like 
} 
1
while (($row = mysqli_fetch_assoc($result)) == true) 

根據該代碼的上述部分。它只會在while循環中進行,如果有任何數據返回或從數據庫中提取,否則如果沒有數據被提取,它將不會進入循環。

如果沒有類別,那麼它將跳出循環,因此這個echo "<td>No categories</td>";將永遠不會顯示。

此外,既然你已經把一個回聲說'沒有類別',我假設它意味着你想回聲輸出,如果沒有類別。但是,你如果條件是錯誤的,因爲,如果u要檢查,如果一些變量是空的,你需要做如下

if(empty($CategoryID['CategoryID'])){ 

     echo "<td>No categories</td>"; 

    } else { 

     echo "<td colspan='4'><img class='profile-photo' src='" . ROOT_URI . "/uploads/" . $Photo . "'></td>"; 
     echo "<td colspan='4'><a href='discussions.php?view={$CategoryID}'>{$Name}</a></td>"; 
     echo "<td><a href='managecategories.php?delete={$CategoryID}'>delete</a></td>"; 
    } 

地方,如果它是空的,如果它不是空if(!empty())將是真正的if(empty())爲真。