Conditional Statements in PHP code Between HTML Code
You probably forgot the endif
of the alternative control structure syntax:
<?php if(check if user is logged in): ?>
<display users profile in html>
<?php else: ?>
<display an error>
<?php endif; ?>
Omitting the braces as you wrote is not possible. It is only possible if it is followed by a regular statement.
If statement inside php content, containing html code
Here's a few ways you can do it:
1: Use a Ternary Operator
<?php
$content = "
<table style='text-align: center;'>
<tr>
<td style='text-align: center;" . ($result->name ? " display: none;" : "") . "'>
{$result->name}
</td>
</tr>
</table>
";
2: Assign the CSS to a variable and interpolate it
<?php
$css = $result->name ? " display: none;" : "";
$content = "
<table style='text-align: center;'>
<tr>
<td style='text-align: center;{$css}'>
{$result->name}
</td>
</tr>
</table>
";
3: Break the $content variable assignment into bits so you can use an if() condition
<?php
$content = "
<table style='text-align: center;'>
<tr>
<td style='text-align: center;";
if($result->name) $content .= " display: none;";
$content .= "'>
{$result->name}
</td>
</tr>
</table>
";
4: Use PHP's template style syntax
<table style='text-align: center;'>
<tr>
<td style='text-align: center;<?php if($result->name): ?> display: none;<?php endif; ?>'>
<?= $result->name; ?>
</td>
</tr>
</table>
This is my preferred option if you're working in a template (a .phtml
file for example).
5: ... or mix it up a bit (template style with a ternary echo)
<table style='text-align: center;'>
<tr>
<td style='text-align: center;<?= $result->name ? " display: none;" : ""; ?>'>
<?= $result->name; ?>
</td>
</tr>
</table>
Ultimately it comes down to which you find most readable and are most comfortable with.
Using PHP conditional statements to switch HTML content on different pages
On your pagetitle.php you could do something like this
<?php
$scriptname = basename($_SERVER["PHP_SELF"]);
if ($scriptname == "about.php") {
echo "<h1>About Page</h1>";
}else if($scriptname == "contact.php"){
echo "<h1>Contact Us</h1>";
}
?>
How to save html code with if condition in database
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();
Related Topics
MySQL Table Does Not Exist Error, But It Does Exist
Bulletin Board - Database Optimisation
Backup a MySQL Database and Download as a File
PHP Call Class Method/Function
Does PHP's $_Request Method Have a Security Problem
What Does a PHP Function Return by Default
[PHP Warning: Mail(): " Sendmail_From" Not Set in PHP.Ini or Custom "From:" Header Missing
Mask Credit Card Number in PHP
Htmlpurifier Iframe Vimeo and Youtube Video
Convert a String to a Double - Is This Possible
Remove Everything from the First Occurrence of a Character to the End of a String in PHP
Simplexml_Load_String() Fail to Parse Error