I need to use IF condition to check whether a PHP variable is equal to some value or not. Here is my try..
while ($row = $result -> fetch_assoc())
{
$qDate .= '<td style="text-align:center"><b>'.$row['Date'].'</td>';
$qStatus .= '<td style="text-align:center">'.$row['Status'].'</td>';
}
Then I need to include a html table into a phpvariable as below.
if($result->num_rows > 0){
$messagenew.= '
<table style="top: 15px; left:10px; " border=1>
<tbody>
<caption style="color: rgb(241, 239, 243);background: rgb(1, 32, 65); ">Status</caption>
<tr>
<td style="text-align:center"><b>Date</td>'.$qDate .'
</tr>
<tr>
<?php if($qStatus=="Not Received"){?>
<td style="text-align:center;background-color: red;"><b>Status</td>'.$qStatus_SEQ .'
<?php}?>
<?php else{ ?>
<td style="text-align:center;background-color: Green;"><b>Status</td>'.$qStatus_SEQ .'
<?php } ?>
</tr>
</tbody>
</table>
';
}
else{
$messagenew.= 'Data is not Available';
}
I have enclosed php code with tags. But still I am not getting the required output. Rather than highlighting the value I can see my Header(Status) have highlighted.And the table were also messed up. Can someone help me on this.
Update:messagenew variable can be seen when mail generated.Mail generating code not attached here.
CodePudding user response:
Since you have mentioned Your Headers are only changing, You better to assign the rule when you are fetching data. Try below..
while ($row = $result -> fetch_assoc())
{
$qDate .= '<td style="text-align:center"><b>'.$row['Date'].'</td>';
if($row['Status']=='Not Received') {
$qStatus .= '<td style="text-align:center;background-color: red;">'.$row['Status'].'</td>';
}
else{
$qStatus .= '<td style="text-align:center;background-color: green;">'.$row['Status'].'</td>';
}
}
CodePudding user response:
you can use variable in if to get required result
if($result->num_rows > 0){
$messagenew.= '
<table style="top: 15px; left:10px; " border=1>
<tbody>
<caption style="color: rgb(241, 239, 243);background: rgb(1, 32, 65); ">Status</caption>
<tr>
<td style="text-align:center"><b>Date</td>'.$qDate .'
</tr>
<tr>';
if($qStatus=="Not Received"){
$messagenew.= '<td style="text-align:center;background-color: red;"><b>Status</td>'.$qStatus_SEQ;
}else{
$messagenew.= '<td style="text-align:center;background-color: Green;"><b>Status</td>'.$qStatus_SEQ;
}
$messagenew.= '</tr>
</tbody>
</table>';
}
else{
$messagenew.= 'Data is not Available';
}
CodePudding user response:
It is better to use the Shorthand If / Else conditional method
if($result->num_rows > 0){
$messagenew.= '
<table style="top: 15px; left:10px; " border=1>
<tbody>
<caption style="color: rgb(241, 239, 243);background: rgb(1, 32, 65); ">Status</caption>
<tr>
<td style="text-align:center"><b>Date</td>'.$qDate .'
</tr>
<tr>
<td style="text-align:center;background-color: '.($qStatus=="Not Received") ? 'red' : 'Green'.';"><b>Status</td>'.$qStatus_SEQ .'
</tr>
</tbody>
</table>
';
}
else{
$messagenew.= 'Data is not Available';
}
CodePudding user response:
if only if statement is needed you may use ?? operator otherwise you may use the condition and concat your table variable e.g.
$table = "<table> ";
if(condition){
$table .= "<tr> <td> lorem ipsum </td></tr>";
}
else{
$table .= "<tr> <td> my heading</td></tr>";
}
CodePudding user response:
Can't you just use curly brackets and double quotes?
if ($result->num_rows > 0)
{
$condition = if ($qStatus == "Not Received") ? "<td style='text-align:center;background-color: red;'><b>Status</td>{$qStatus_SEQ}" : "<td style='text-align:center;background-color: Green;'><b>Status</td>{$qStatus_SEQ}";
$messagenew.= "
<table style='top: 15px; left:10px;' border=1>
<tbody>
<caption style='color: rgb(241, 239, 243);background: rgb(1, 32, 65);'>Status</caption>
<tr>
<td style='text-align:center'><b>Date</td>{$qDate}
</tr>
<tr>
{$condition}
</tr>
</tbody>
</table>
";
}
else
{
$messagenew.= 'Data is not Available';
}