How to save html code with if condition in database

I have a rating field in the database. If the value of $rating>0, the button should be disabled otherwise button should be enabled.I am saving the HTML code to database to dispaly.

What I have tried is :

 $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();

The data is saving but What I get in my output as:

How to save html code with if condition in database

How to avoid if and else from showing in html?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The problem with your code is that you are not closing the string before you add the condition to append to the rest of it. Also, variables do not need to be escaped in strings using double quotes.

$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();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x