2011-10-27 7 views
0

私はクラスRankingsDateFilterを作成しました。PHP/SQL - 特定の日付の後に作成された行を表示

各ランキングクラスには、カットオフ日付を生成するクラス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日前にアップロードされたとは思うが、表示されている。

答えて

1

はMySQLに渡している日付の周りにいくつかの引用符で投げ、そしてあなたのカラム名の前後に引用符をドロップ:

return "'" . $limitDate->format('Y-m-d') . "' < created";