0

I want to show the records from database daywise from start dat to end date...the records has date which is inserted to the database at the record insertion time...the records are not stored daily so there must be a gap between two dates record. So now I want to show the records daywise that is if the perticular date has no records i want to show 0 instead of it.

I am able to show the records which date is stored in database. But cant show the records which date are not available in the database.

>$res=mysql_query("SELECT DISTINCT Date FROM TABLE_NAME WHERE Date BETWEEN startdate AND enddate");
>while($row = mysql_fetch_array($res))
>{
>  $date[] .= $row['Date'];
>}
>
>foreach($date as $da)
>{
>  $query = "SELECT * FROM TABLE_NAME WHERE Date = '".$da."' ";
>  $result = mysql_query($query);
>  while($row = mysql_fetch_array($result))
>  {
>   $prod = $row['No_of_prod'];
>  }
>  echo $prod;      
>}
4

1 に答える 1

1

毎日空のエントリをデータベースに入れてフェッチするか、PHPコードで、開始日から終了日に達するまでの日付としてキーを持つ配列を作成し、その値はゼロ(0)になります。データベース レコードをフェッチした後、 array_merge 関数を使用して、生成された最終的な配列を使用してデータを表示できます。次の日付範囲を作成するためのリクエストのコードは次のとおりです

$dt_start = new DateTime('20090101');
$dt_end   = new DateTime('20090222');
$arrDates[] = $dt_start->format('M');
while ($dt_start->modify('1 day') <= $dt_end) {
    $arrDates[$dt_start->format('Y-m-d')] = 0;  // Or whatever you want to do with it.
}

$arrDates日付範囲が含まれています。

于 2013-06-06T10:38:06.303 に答える