dsnd7200 2017-07-23 00:55
浏览 9
已采纳

如果一个值存储在数据库中,我可以回显它,但如果没有存储值,我怎么能省略整个标记?

echo "<a href=\"{$row['link']}\" class=\"button\">Website link</a>";

This will display an an anchor tag with a class of "button" and the href value will be whatever is in the database. If I don't have a link stored in the database, I want nothing to appear in the rendered html, but with this example the button will still show up, it just won't have an href value.

I've tried doing this:

echo "{$row['link']}";

And in the database, the 'link' value would be:

<a href="#" class="button">Website link</a>

My issue with this is that I don't like storing and pulling html code from the database. Also, if I leave the link value empty in the database, part of the button is still visible because of the "button" class.

  • 写回答

3条回答 默认 最新

  • dounou9751 2017-07-23 00:59
    关注

    It sounds like all you need is a very simple conditional.

    You have a column in your table for the link so regardless of whether it has a value or not, it will be in the array.

    Add an if statement to check it.

    if ( $row['link'] ) {
        echo "<a href=\"{$row['link']}\" class=\"button\">Website link</a>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?