2012-02-23 103 views
0

我試圖解決一個問題,但沒有成功。小計根據日期

我有一個索引頁和用戶搜索。例如

enter image description here

我想要做的是有分類彙總。我的意思是我想根據一天的總數。

例如用戶搜索2012-02-22和2012-02-23,但我需要總月份的22和23。

爲了做到這一點:我這樣做是計算在T

while ($info = mysql_fetch_array($dbResult)) { 
                $total+= $info['totalEvents']; 
                $total2 = 0; 
                echo "<tr>"; 
                echo "<td><input type=checkbox name='check1' id='check1' value='" . $info['eventCategory'] . "' onclick=recal(" . $info['totalEvents'] . ",this.checked) checked></td> 
                <label id ='nameID'><td>" . $info['id'] . "  " . $info['profileName'] . "</td>"; 
                echo "<td><label id='eventCategory'>" . $info['eventCategory'] . "</td>"; 
                echo "<td><label id='totalEvents'>" . $info['totalEvents'] . "</label></td>"; 

                if ($info['Date'] != $previousDate) { 
                 echo "<td><b>" . $info['Date'] . "</b></td></tr>"; 
                 $total2++; 
                } else { 
                 echo "<td></td>"; 
                } 
                $previousDate = $info['Date']; 
               } 

但它計算像

enter image description here

終於我來到這 但我計算錯誤

else if ($_POST['group1'] == 'VideoFinish') { 
               $dbResult = mysql_query("SELECT la.id,st.profileName, la.totalEvents,la.Date,ft.eventCategory FROM videofinish la INNER JOIN profiles st ON st.id=la.id INNER JOIN eventcategory ft ON ft.id = la.eventCategoryID where Date BETWEEN '" . $startDate . "' and '" . $endDate . "'"); 

               if (isset($_POST['checkPremium'])) { 
                $dbResult = mysql_query("SELECT la.id,st.profileName, la.totalEvents,la.Date,ft.eventCategory FROM videofinish la INNER JOIN profiles st ON st.id=la.id and st.isPremium=1 INNER JOIN eventcategory ft ON ft.id = la.eventCategoryID where Date BETWEEN '" . $startDate . "' and '" . $endDate . "'"); 
               } 

               $previousDate = ''; 
               $totalDay = 0; 
               while ($info = mysql_fetch_array($dbResult)) { 
                $total+= $info['totalEvents']; 
                echo "<tr>"; 
                echo "<td><input type=checkbox name='check1' id='check1' value='" . $info['eventCategory'] . "' onclick=recal(" . $info['totalEvents'] . ",this.checked) checked></td> 
                <label id ='nameID'><td>" . $info['id'] . "  " . $info['profileName'] . "</td>"; 
                echo "<td><label id='eventCategory'>" . $info['eventCategory'] . "</td>"; 
                echo "<td><label id='totalEvents'>" . $info['totalEvents'] . "</label></td>"; 

                if ($info['Date'] != $previousDate && $info['Date'] != $previousDate) { 
                 echo "<td><b>" . $info['Date'] . "</b></td></tr>"; 
                 echo "<b>".$info['Date']."</b>:"; 
                 echo $totalDay."<br />"; 
                } else { 

                 echo "<td></td>"; 
                } 
                $previousDate = $info['Date']; 
                $totalDay += $info['totalEvents']; 


               } 
               echo "<td><b id='total'>" . $total . "</b></td>"; 

              } 

enter image description here

+0

請出示在$ dbResult您的數據庫查詢。您在查詢中的計算可能是錯誤的。 – iamsupergrasya 2012-02-23 07:36:42

回答

1

試試這個..我把你的理解代碼的意見。

// initialize counter and previous date 
$totalDay = 0; 
$previousDate = NULL; 

// we need to change the loop structure so that the loop code is executed 
// a last time when there are no further rows. Otherwise, we would have 
// no total on the last day! 
while (true) { 
    // we break the loop when there are no further rows after sub total is printed 
    $info = mysql_fetch_array($dbResult); 

    // when date changes, print total line and reset counter 
    if ($previousDate <> NULL && 
     (!isset($info['Date']) || $info['Date'] <> $previousDate)) { 
     echo '<tr>'; 
     // no checkbox 
     echo '<td></td>'; 
     // label in second cell 
     echo '<td><b>Total for day ' . $previousDate .'</b></td>'; 
     // total in last cell 
     echo '<td colspan="4" align="right"><b>' . $totalDay . '</b></td>'; 
     echo '</tr>'; 
     // day has changed, so reset the sub total for next day 
     $totalDay = 0; 
    } 

    // now if the have no further rows, break the loop 
    if ($info == false) 
     break; 

    // add current value to sub total 
    $totalDay += $info['totalEvents']; 

    echo "<tr>"; 
    echo "<td><input type=checkbox name='check1' id='check1' value='" . $info['eventCategory'] . "' 
      onclick=recal(" . $info['totalEvents'] . ",this.checked) checked></td>"; 
    echo "<label id ='nameID'><td>" . $info['id'] . "  " . $info['profileName'] . "</td>"; 
    echo "<td><label id='eventCategory'>" . $info['eventCategory'] . "</td>"; 
    echo "<td><label id='totalEvents'>" . $info['totalEvents'] . "</label></td>"; 

    echo "<td>";  
    // when date changes (also on first day), print date 
    if ($info['Date'] <> $previousDate) { 
     echo "<b>" . $info['Date'] . "</b>"; 
    } 
    echo "</td>"; 
    echo "</tr>"; 

    // remember current date for next row calculation 
    $previousDate = $info['Date']; 
} 
+0

已更新,包括最後一天的總數.. – Kaii 2012-02-23 07:57:09

+0

不打印任何東西:( – 2012-02-23 07:57:46

+0

@MertMETİN你的意思是「nothing」?白頁?​​請檢查你的錯誤日誌..也許我有一個語法錯誤的地方..不能測試這個代碼; ) – Kaii 2012-02-23 08:00:04

0

你可以在你的mysql語句中的GROUP BY your_date_field WHERE your_date_field(22,23)`中。

+0

如果22之間25我想計算22,23 24,25 – 2012-02-23 07:21:03

+0

@MertMETİN你可以使用之間的mysql子句。 – linuxeasy 2012-02-23 08:11:17

0

嘗試此查詢

select * from table 
GROUP BY date_column 
WHERE date_column between "2001-01-05" and "2001-01-10"