If else statement in function

I have this function which gets list from database and displays it in the table on the page. However, I need to input if statement in front of $row[“title”] which checks if $row[“mydate”] is within one month of today’s date, then display “New”, otherwise don’t display anything.

Basically, any lectures which are within 30 days from today should show New in front of the title.

How do I achieve this? I tried inputting if statement there, but it display that whole if statement as a text there.

 function uk()
    {
    	$output = '';
    	$result = db_query("SELECT * FROM lecture where groups='uk'");

    	if(mysqli_num_rows($result) > 0)
    	{
    		$output .= '<div id="style2" style="overflow-x:auto;">
    						<table>
    							<tr>
    								<th class="text-center">Date</th>
    								<th class="text-center">Title</th>
    								<th class="text-center">Venue</th>
    								<th class="text-center">Duration</th>
    								<th class="text-center">Size (MB)</th>
    								<th class="text-center">Link</th>
    							</tr>';
    		while($row = mysqli_fetch_array($result))
    		{
    			$output .= '
    				<tr>
    					<td>'.$row["mydate"].'</td>
    					<td>'.$row["title"].'</td>
    					<td>'.$row["venue"].'</td>
    					<td>'.$row["duration"].'</td>
    					<td>'.$row["size"].'</td>
    					<td><a href="../'.$row["path"].$row["file_name"].'">Save</a></td>
    				</tr>
    			';
    		}
    		echo $output;
    	}
    	else
    	{
    		echo 'Data Not Found';
    	}
    }

Everything between quotes is text. Most simple solution would be to use the DateTime::diff method to get a DateInterval object which holds the difference between the two dates.

 function uk()
    {
    	$output = '';
    	$result = db_query("SELECT * FROM lecture where groups='uk'");

    	if(mysqli_num_rows($result) > 0)
    	{
    		$output .= '<div id="style2" style="overflow-x:auto;">
    						<table>
    							<tr>
    								<th class="text-center">Date</th>
    								<th class="text-center">Title</th>
    								<th class="text-center">Venue</th>
    								<th class="text-center">Duration</th>
    								<th class="text-center">Size (MB)</th>
    								<th class="text-center">Link</th>
    							</tr>';
    		while($row = mysqli_fetch_array($result))
    		{
    			$date = new DateTime($row["mydate"]);
    			$interval = $date->diff(new DateTime());

    			if($interval->y == 0 && $interval->m == 0) {
    			    $row["title"] = '[NEW] ' . $row["title"];
    			}

    			$output .= '
    				<tr>
    					<td>'.$row["mydate"].'</td>
    					<td>'.$row["title"].'</td>
    					<td>'.$row["venue"].'</td>
    					<td>'.$row["duration"].'</td>
    					<td>'.$row["size"].'</td>
    					<td><a href="../'.$row["path"].$row["file_name"].'">Save</a></td>
    				</tr>
    			';
    		}
    		echo $output;
    	}
    	else
    	{
    		echo 'Data Not Found';
    	}
    }

Or you could distinguish older rows from newer rows with the help of MySQL.

SELECT *, mydate BETWEEN DATE_SUB(NOW(), interval 1 MONTH) AND NOW() AS new FROM lecture WHERE groups=‘uk’

if($row["new"]  == '1') {
    $row["title"] = '[NEW] ' . $row["title"];
}
Sponsor our Newsletter | Privacy Policy | Terms of Service