Jump to content

php If statement inside table


cjkeane

Recommended Posts

Hi everyone.

 

i'm trying to write an if statement for several scenerios

1. if the date entered in '$DateDue' is overdue, meaning greater than todays date, then display the word 'Late'.

2. if the date entered in '$DateDue' is not yet past, meaning its greater than todays date, then display the word 'ok'.

3. if the date entered in '$DateDue' is within a week of todays date, then display the word 'warning'

 

the code snippet below is my partial script. it runs perfectly without embedding the 'php if statement' on the first td row. So my problems are:

1. how to embed a php if statement in table results and

2. have OK, LATE or WARNING to be displayed based on if $DateDue expires or not.

there are no errors in the code below, it just echos the line '<?php if ($DataDue > CURDATE()) {echo "OK";} elseif ($DateDue < CURDATE()) {echo "LATE";} else {echo "Warning";}?>' instead of displaying OK, LATE or Warning.

 

I'd appreciate any assistance you could provide. thanks.

 

           
// echo out the contents of each row into a table
echo "<tr>";
echo '<td nowrap><?php if ($DataDue > CURDATE()) {echo "OK";} elseif ($DateDue < CURDATE()) {echo "LATE";} else {echo "Warning";}?>' . mysql_result($result, $i, 'DateDue') . '</td>';
echo '<td nowrap>' . mysql_result($result, $i, 'Description') . '</td>';
echo '<td nowrap>' . mysql_result($result, $i, 'CompanyName') . '</td>';    
echo "</tr>"; 

Link to comment
Share on other sites

you cannot put php tags inside echo statement. If you are using echo, that means php tags are already opened.

 

// echo out the contents of each row into a table
echo "<tr>";
echo '<td nowrap>';
if ($DataDue > CURDATE()) {echo "OK";} elseif ($DateDue < CURDATE()) {echo "LATE";} else {echo "Warning";}
echo mysql_result($result, $i, 'DateDue') . '</td>';
echo '<td nowrap>' . mysql_result($result, $i, 'Description') . '</td>';
echo '<td nowrap>' . mysql_result($result, $i, 'CompanyName') . '</td>';    
echo "</tr>"; 

Link to comment
Share on other sites

php code blocks never really end.  so long as the script has access to the assigned variables and arrays (either by including another page/script or on the same page) then you will have access to it later.  this allows for a mixture of languages simultaneously, so long as they don't butt heads.  take for example:

 

<?php 
$a = 5;
?>
<table>
<tr><td><?php echo $a;?></td></tr>
</table>
<?php
$a - 5;
?>
<div id="#stuf"><?php echo $b;?></div>

 

Good Luck!

Link to comment
Share on other sites

  • 3 weeks later...

I'm getting close. I've re-worked my code and now it displays the status of the date next to it for e.g.

Pay bill | Late | 2011/03/28

Pay bill | Late | 2011/03/24

Pay bill | Late | 2011/03/23

etc.

 

my issue is that is always displays the word 'Late' no matter if the 'DateDueInput' is in the past, close the todays date or even in the future. i believe my if statement is correct, but i'd appreciate it if someone could point me in the right direction as to why it only displays the word 'Late' instead of Late, Warning or OK.

 

Thanks.

 

<?php
        $string ="SELECT Description, DateDueInput from records WHERE ID='$ID'";
        $query = mysql_query($string) or die (mysql_error());
$result = mysql_fetch_array($query);
$today = date("Y-m-d"); 

if ($result['DateDueInput'] < $today) {
	// date is in the past
	$DueDate = '<font color="#FF0000">Late</font>';
} 
elseif ($result['DateDueInput'] <= $today+5) {
	// date is close to current date
	$DueDate = '<font color="#FFFF00">Warning</font>';

} else {
// date hasn't passed yet, so display OK.
	$DueDate = '<font color="#00FF00">OK</font>';
}

if($result==true)
{
do 
{ 
echo '</table>';
        echo '<tr>';
echo '<td nowrap>' . $result['Description'] . '| </td>';
echo '<td nowrap>' . $DueDate . '| </td>';
        echo '<td nowrap>' . $result['DateDueInput'] . '| </td>';
echo '</tr>'; 
    }
While($result = mysql_fetch_array($query));
}                
        // close table>
        echo '</table><hr>'; 
?>

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.