2015-02-11 68 views
1

正在尋找一個解決方案,以下編碼位。如果數據庫字段爲空回顯「沒有」,如果有東西回顯「東西」

<?php 

$nextfive_events = mysql_query("SELECT date_format(date, '%d/%m/%Y') AS formatted_date, title, location, regs FROM events WHERE status = 'ACTIVE'"); 
if(mysql_num_rows($nextfive_events) == 0) { echo "<p>No events coming up!"; } else { 

echo "<table width=\"600\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"greywritinglight\" align=\"left\"> 
<tr align=\"center\"> 
<td>Date</td> 
<td>Name</td> 
<td>Location</td> 
<td></td> 
</tr>"; 

$x=1; 
while($next_row = mysql_fetch_array($nextfive_events)) 

{ 

    if($x%2): $rowbgcolor = "#FFFFFF"; else: $rowbgcolor = "#EEEEEE"; endif; 
echo "<tr align=\"center\">"; 
echo "<td>" . $next_row['formatted_date'] . "</td>"; 
echo "<td>" . $next_row['title'] . "</td>"; 
echo "<td>" . $next_row['location'] . "</td>"; 
echo "<td><a href=\"regs/" . $next_row['regs'] . "\">Regs</a></td>"; 
echo "</tr>"; 
$x++; 
} 
echo "</table>"; 
} 
?> 

我想行echo "<td> <a href regs .....

要顯示的字的REG時有東西在數據庫中的「暫存器」。說如果該領域沒有任何東西,我希望它是空白的,而不是說Regs。

感謝

+0

'。 (isset($ next_row ['regs'])|| empty($ next_row ['regs']))''nothing':'something'. – Alex 2015-02-11 23:33:03

回答

1

你可以做三元操作符:

echo "<td><a href='" . (empty($next_row['regs']) ? "#" : $next_row['regs']) . "'>Regs</a></td>"; 

儘量不要做轉義字符,他們看起來很奇怪,對於href屬性做單引號。此外,您是否想要

<a href='#'>Regs</a> 

要顯示它是否爲空?

如果沒有,試試這個:

echo (!empty($next_row['regs']) ? "<td><a href='" . $next_row['regs'] . "'>Regs</a></td>" : ""); 
+0

@JosephThomasMallinson我更新了我的答案,將href更改爲回顯動態行值而不是隻是'regs' – 2015-02-11 23:54:04

+0

@JosephThomasMallinson它需要分號前的最後一個引號。對不起,直到我把它放到文本編輯器中時才知道。 – 2015-02-12 00:00:45

+0

再試一次,我改變了括號的位置,所以它應該工作。我很抱歉編輯,很難調試我沒有使用的PHP。 – 2015-02-12 00:10:59

1

你可以使用一個三元:

echo (! empty($next_row['regs'])) ? '<td><a href=\"regs/" . $next_row['regs'] . "\">Regs</a></td>' : '<td>&nspb;</td>'; 
0

首先,我想告訴你用PHP一個非常方便的技巧,可以讓你回聲出到HTML,而無需實際回聲。只需在您的PHP代碼中創建一箇中斷,並且在您放置的任何內容之間都可以作爲HTML回顯。

至於返回的行數,你正確地做到了。確保正確查詢,建立了連接,並且SQL中沒有邏輯錯誤。

對不起,沒有注意到你想檢查列是空的!只需使用函數empty()。當你給它的數據是空的,這個函數將返回true,所以簡單地給它一個你希望檢查的數據行的列。

<?php 

    // Lets say this is your database connection variable 
    $connection = mysqli_connect(//Your info goes here); 

    // This is the query you want to run 
    $query = "SELECT something FROM somewhere WHERE something='$something_else'"; 

    // This is where you are storing your results of the query 
    $data = mysqli_query($connection, $query); 

    // Here, you can ask for how many rows were returned 
    // In PHP, ZERO is evaluated to false. We can use a 
    // Short-hand boolean expression like this 
    if (mysqli_num_rows($data)) 
    { 

     // Because zero evaluates to false, we know 
     // that if we get HERE that there ARE rows 

     // We can break out of PHP and into our HTML 
     // by simply ending our PHP tag! Just remember 
     // that you need to open it back up again though! 

     ?> 

      <!--You can put your HTML here--> 

      <p> 

       We found some rows that you might 
       need to know about! 

      </p> 

     <?php 

    } 
    else 
    { 

     // But, if we get here, we know it evaluated as 
     // FALSE and therefore no rows returned 

     // Here's that HTML trick again 

     ?> 

      <!--HTML can go here--> 
      <p> 

       No rows returned! 

      </p> 

     <?php 

    } 

?>