2017-05-16 36 views
1

私は日付と値を持つテーブルを持っている、いないすべての日付が利用可能なので、そうでないところ、私はゼロを表示する必要があります。PHP&MySQLの、ショーの空の日

$this->db->limit($days); 
    $dati = $this->db->query('SELECT sum(eur_80) as eur_80, day_stamp FROM table WHERE user_id = ? GROUP BY day_stamp ORDER BY rendi_id DESC', 119)->result(); 

    echo "periodo,guadagno" . PHP_EOL; 

    $date = date('Y-m-d', strtotime('-' . $days . ' days')); 
    for ($d = 0; $d <= $days - 1; $d++) { 
     $day = date('Y-m-d', strtotime($date . " +" . $d . " day")); 
     foreach ($dati as $stats => $stat) { 
      if ($stat->day_stamp === $day) { 
       echo $stat->day_stamp . "," . decimali($stat->eur_80) . PHP_EOL; 
      } else { 
       echo date('Y-m-d', strtotime($date . " +" . $d . " day")) . ",0.0" . PHP_EOL; 
      } 
     } 
    } 

これは、多くの行を返します。

periodo,guadagno 
2017-05-09,0.0 
2017-05-09,0.5385 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-09,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-10,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-11,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-12,0.0 
2017-05-13,0.5680 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-13,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-14,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 
2017-05-15,0.0 

私が必要とするのは、日付ごとに1つのアイテムのみを表示することです。間違っていますか?

+0

「DISTINCT day_stamp'のようなものですか? – Edwin

答えて

2

データベースにアイテムがあるかどうか、出力を0にしなければならない人が見つかった場合のみ覚えておく必要があります。

$this->db->limit($days); 
$dati = $this->db->query('SELECT sum(eur_80) as eur_80, day_stamp FROM table WHERE user_id = ? GROUP BY day_stamp ORDER BY rendi_id DESC', 119)->result(); 

echo "periodo,guadagno" . PHP_EOL; 

$date = date('Y-m-d', strtotime('-' . $days . ' days')); 
for ($d = 0; $d <= $days - 1; $d++) { 
    $day = date('Y-m-d', strtotime($date . " +" . $d . " day")); 
    $dayFound = false; 
    foreach ($dati as $stats => $stat) { 
     if ($stat->day_stamp === $day) { 
      echo $stat->day_stamp . "," . decimali($stat->eur_80) . PHP_EOL; 
      $dayFound = true; 
     } 
    } 
    if (!$dayFound) 
      echo date('Y-m-d', strtotime($date . " +" . $d . " day")) . ",0.0" . PHP_EOL; 
} 
+0

そうですよ!ありがとう –