我创建了课程Rankings
和DateFilter
。
每个排名类都有一个DateFilter
类,应该产生截止日期。我正在尝试创建一个过滤器,以便在该日期之后创建的所有内容都将显示在表格中。
但是,比较不起作用。你能看到问题吗?
这是我的DateFilter类:
<?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'";
}
}
?>
我的排名等级:
<?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;
}
}
?>
我这样称呼它:
$test = new DateFilter(100);
$rankings = new Rankings($test);
$rankings->display();
在该示例中,没有显示任何内容,即使我确定我的日期库中的所有内容都是在不到100天前上传的。
答案 0 :(得分:1)
在您传递给MySQL的日期附近引用一些引号,并删除列名称周围的引号:
return "'" . $limitDate->format('Y-m-d') . "' < created";