【问题标题】:How to save html code with if condition in database [duplicate]如何在数据库中保存带有if条件的html代码[重复]
【发布时间】:2021-12-06 01:56:54
【问题描述】:

我在数据库中有一个评分字段。如果 $rating>0 的值,则应禁用该按钮,否则应启用该按钮。我将 HTML 代码保存到数据库以显示。

我试过的是:

 $user_notification->notification ="Delivery date <b>" .$date."</b> and delivery time <b> ".$time." </b> for the truck " .$truck_name. " of mileage ".$mileage_name. " of quantity ".$qty. " has confirmed 
        <br> <a href='#' class='btn btn-primary' >Pay ?</a>
        if(.$rating.'=='.0.'){<a href='/showTransaction/".$buy_id."' class='btn btn-primary' >Review ?</a>}else{<a href='' class='btn btn-primary' disabled >Review ?</a>}";
         $user_notification->save();

数据正在保存,但我在输出中得到的是:

如何避免 if 和 else 在 html 中显示?

【问题讨论】:

    标签: php html mysql database if-statement


    【解决方案1】:

    您的代码的问题在于,在添加条件以附加到字符串的其余部分之前,您没有关闭字符串。此外,变量不需要在字符串中使用双引号进行转义。

    $user_notification->notification = "Delivery date <b>$date</b> and delivery time <b>$time</b> for the truck $truck_name of mileage $mileage_name of quantity $qty has confirmed<br><a href='#' class='btn btn-primary'>Pay ?</a>";
    
    if ($rating == 0) {
        $user_notification->notification .= "<a href='/showTransaction/$buy_id' class='btn btn-primary'>Review ?</a>";
    } else {
        $user_notification->notification .= "<a href='' class='btn btn-primary' disabled>Review ?</a>";
    }
    
    $user_notification->save();
    

    【讨论】:

    • 刷新页面后,按钮没有被禁用。
    猜你喜欢
    • 2016-02-25
    • 2018-01-10
    • 2019-04-27
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多