我尝试解决问题,但没有成功。

我有一个索引页面,用户可以搜索。例如



我想做的是小计。我的意思是我想根据天总计。

例如用户搜索2012-02-22和2012-02-23,但我需要总计22个月和23个月。

为此:我这样做是基于

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'];
                                                }


但它像这样计算



终于我来到了
但是我算错了

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>";

                                            }

最佳答案

尝试这个..我在代码中添加了注释,以供您理解。

// 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'];
}

关于php - 按日期分类的小计,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9408628/

10-11 21:54