I created classes Rankings and DateFilter.
Each rankings class has a DateFilter class that should produce the cutoff date. I am trying to be able to create a filter so that everything created after that date will be displayed in a table.
However, the comparison does not work. Can you see a problem?
Here is my DateFilter class:
<?php
include ("Filter.php");
class DateFilter extends Filter
{
//@param daysOld: how many days can be passed to be included in filter
//Ex. If daysOld = 7, everything that is less than a week old is included
private $interval;
public function DateFilter($daysOld)
{
$this->interval = new DateInterval('P'.$daysOld.'D');
}
//@Return: Returns a DateTime that is the earliest possible date to be included in the filter
function createLimitDate()
{
$now = new DateTime();
return $now->sub($this->interval);
}
//generates SQL code for checking date
//Ex. WHERE limitDate > created... if > means before
function genSQL()
{
$limitDate = $this->createLimitDate();
return $limitDate->format('Y-m-d') . " < 'created'";
}
}
?>
And my Rankings Class:
<?php
class Rankings
{
private $filter;
//@params: $filty is the filter given to these rankings
public function Rankings($filty)
{
$this->filter = $filty;
}
//@return: returns the html code for the rankings
public function display()
{
echo '<table border="1" align="center">'.
'<tr align="center" style="font-weight:bold;">
<b><td>#</td><td>NAME</td><td>Date</td></b>
</tr>
';
//hardcoding DB
$where = $this->filter->genSQL();
$qry = mysql_query("SELECT * FROM `pix`
WHERE $where
");
if (!$qry)
die("FAIL: " . mysql_error());
$i = 1;
while($row = mysql_fetch_array($qry))
{
$name = $row['uniquename'];
$created = $row['created'];
echo ' <tr>
<td>'. $i . '</td>'.
'<td>' . $name . '</td>'.
'<td>'. $created . '</td>'.
'</tr>';
$i += 1;
}
echo '</table>';
echo $where;
}
}
?>
I’m calling it like this:
$test = new DateFilter(100);
$rankings = new Rankings($test);
$rankings->display();
In that example, nothing is displayed, even though I’m sure everything in my datebase was uploaded less than 100 days ago.
Throw in some quotes around the date you’re passing to MySQL, and drop the quotes around your column name: