2015-02-11 80 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 

    } 

?>