如果数据库字段为空,则回显“无”,如果有某些内容回显“某事”

时间:2015-02-11 23:28:22

标签: php

寻找下面这段编码的解决方案。

    <?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 .....

当数据库中的“regs”中存在某些内容时显示单词Regs。如果该字段中没有任何内容,我希望它是空白的,而不是说Regs。

感谢

3 个答案:

答案 0 :(得分:1)

你可以使用三元:

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

答案 1 :(得分: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>" : "");

答案 2 :(得分: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

    }

?>
相关问题