如何在 html table(日历)中将行值加在一起?

How to add row values together in a html table (calendar)?

我有一个使用 html table 用 php 制作的日历。

每个日期都有一个从数据库中插入到天数下方的整数值。

我想要做的是获取 table 的每一行(周)的总和并将其放入第 8 列。我怎样才能做到这一点?

有什么帮助吗?

    <?php

$conn = mysqli_connect('localhost','username','password','database_name');
?>
<html>
<head>
<title>
Calendar
</title>
<link rel="stylesheet" href="/test/style.css">
<script src="/test/script.js"></script>
</head>
<body>

<?php

//This gets today's date
$date = time() ;

//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date);
$year = date('Y', $date);



//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day) ; 
switch($day_of_week){ 
    case "Sun": $blank = 0; break; 
    case "Mon": $blank = 1; break; 
    case "Tue": $blank = 2; break; 
    case "Wed": $blank = 3; break; 
    case "Thu": $blank = 4; break; 
    case "Fri": $blank = 5; break; 
    case "Sat": $blank = 6; break; 
} 
//We then determine how many days are in the current month 
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads 
echo "<table border=1 width=294>"; 
echo "<tr><th colspan=7> $title $year </th></tr>"; 
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td>
<td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>Total:</td></tr>"; 
$day_count = 1; 
$row_number = 1;
echo "<tr id='row" . $row_number . "'>"; 
$row_number++;
//first we take care of those blank days 

/////////get beginning of month
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank;
$last_month_end = $last_month_days_in_month-$last_month_days_to_add_to_last_month_end;
//end ^^


while ( $blank > 0) { 

echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>"; 
$last_month_end++;
$blank = $blank-1; 
$day_count++; 
}
//sets the first day of the month to 1 
$day_num = 1; 
//count up the days, untill we've done all of them in the month
$week_total_mileage = array();
$x = 0;
while ( $day_num <= $days_in_month ) {
//get total miles from database
    $getDay = $year . "-" . $month . "-" . $day_num;
    $query = "SELECT * FROM table WHERE date='" . $getDay . "'";
    $doQuery = mysqli_query($conn,$query);
        while($rows = mysqli_fetch_assoc($doQuery)) {
            $total_miles = $rows['total_miles'];
        }
    $num_rows = mysqli_num_rows($doQuery);

    echo "<td id='" . $day_count . $row_number . "' value='" . $total_miles . "' 
    >
    <form method='post' action='/test/day.php'>
    <input type='hidden' value='" . $day_num . "' name='day'>
    <input type='hidden' value='" . $title . "' name='month'>
    <input type='hidden' value='" . $year . "' name='year'>
    <input type='button' id='dayNum' value='" . $day_num . "'>
    </form>
    <span id='totalMiless'>Total miles: ";
        if($num_rows == 1) { 
            echo $total_miles;
        }
        else {
            echo '-';
        }
    echo "</span>
    </td>
    <div class='hiddenDay' id='" . $day_num . $title . $year . "' style='display:none'>
    <span id='totalMiles'>Total miles: ";
        if($num_rows == 1) { 
            echo $total_miles;
        }
        else {
            echo '0';
        }
    echo "</span></div>"; 
    $week_total_mileage[$day_num] = $total_miles;
    $day_num++;
    $day_count++; 

        if ($day_count > 7) { 
            $total_total = 0;
            while($x < 8) {
                $total_total = $total_total + $week_total_mileage[$x];
                $x++;
            }
            while($x < 14 && $x > 8) {
                $total_total = $total_total + $week_total_mileage[$x];
            }
            echo "<td>" . $total_total . 
            "</td></tr><tr id='row" . $row_number . "'>"; 
            empty($week_total_mileage);
            $day_count = 1; 
            $row_number++;
        } 

}
//Finaly we finish out the table with some blank details if needed  
$end_days = 1;
while ( $day_count >1 && $day_count <=7 )   {   
echo "<td><span id='endDays'>" . $end_days . "</span></td>";   
$day_count++;   
$end_days++;
}   
echo "</tr></table>";
?>
</body>
</html>

以上代码输出如下:

I just went through your script, put all total miles into an array and calculate sum with array_sum

试试这个代码:

     <?php

$conn = mysqli_connect('localhost','username','password','database_name');
?>
<html>
<head>
<title>
Calendar
</title>
<link rel="stylesheet" href="/test/style.css">
<script src="/test/script.js"></script>
</head>
<body>

<?php

//This gets today's date
$date = time() ;

//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date);
$year = date('Y', $date);



//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month falls on 
$day_of_week = date('D', $first_day) ; 
switch($day_of_week){ 
    case "Sun": $blank = 0; break; 
    case "Mon": $blank = 1; break; 
    case "Tue": $blank = 2; break; 
    case "Wed": $blank = 3; break; 
    case "Thu": $blank = 4; break; 
    case "Fri": $blank = 5; break; 
    case "Sat": $blank = 6; break; 
} 
//We then determine how many days are in the current month 
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads 
echo "<table border=1 width=294>"; 
echo "<tr><th colspan=7> $title $year </th></tr>"; 
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td>
<td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>Total:</td></tr>"; 
$day_count = 1; 
$row_number = 1;
echo "<tr id='row" . $row_number . "'>"; 
$row_number++;
//first we take care of those blank days 

/////////get beginning of month
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank;
$last_month_end = $last_month_days_in_month-$last_month_days_to_add_to_last_month_end;
//end ^^


while ( $blank > 0) { 

echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>"; 
$last_month_end++;
$blank = $blank-1; 
$day_count++; 
}
//sets the first day of the month to 1 
$day_num = 1; 
//count up the days, untill we've done all of them in the month
$week_total_mileage = array();
$weekly_total = array();
$x = 0;
while ( $day_num <= $days_in_month ) {
//get total miles from database
    $getDay = $year . "-" . $month . "-" . $day_num;
    $query = "SELECT * FROM table WHERE date='" . $getDay . "'";
    $doQuery = mysqli_query($conn,$query);
        while($rows = mysqli_fetch_assoc($doQuery)) {
            $total_miles = $rows['total_miles'];
        }
    $num_rows = mysqli_num_rows($doQuery);

    echo "<td id='" . $day_count . $row_number . "' value='" . $total_miles . "' 
    >
    <form method='post' action='/test/day.php'>
    <input type='hidden' value='" . $day_num . "' name='day'>
    <input type='hidden' value='" . $title . "' name='month'>
    <input type='hidden' value='" . $year . "' name='year'>
    <input type='button' id='dayNum' value='" . $day_num . "'>
    </form>
    <span id='totalMiless'>Total miles: ";
        if($num_rows == 1) { 
            echo $total_miles;
        }
        else {
            echo '-';
        }
    echo "</span>
    </td>
    <div class='hiddenDay' id='" . $day_num . $title . $year . "' style='display:none'>
    <span id='totalMiles'>Total miles: ";
        if($num_rows == 1) { 
            echo $total_miles;
        }
        else {
            echo '0';
        }
    echo "</span></div>"; 
    if(is_numeric($total_miles)) {
        $weekly_total[] = $total_miles;
    }
    $week_total_mileage[$day_num] = $total_miles;
    $day_num++;
    $day_count++; 

        if ($day_count > 7) { 
            $total_total = 0;
            while($x < 8) {
                $total_total = $total_total + $week_total_mileage[$x];
                $x++;
            }
            while($x < 14 && $x > 8) {
                $total_total = $total_total + $week_total_mileage[$x];
            }
            echo "<td>" . array_sum($weekly_total) . 
            "</td></tr><tr id='row" . $row_number . "'>"; 
            empty($week_total_mileage);
            $weekly_total = array();
            $day_count = 1; 
            $row_number++;
        } 

}
//Finaly we finish out the table with some blank details if needed  
$end_days = 1;
while ( $day_count >1 && $day_count <=7 )   {   
echo "<td><span id='endDays'>" . $end_days . "</span></td>";   
$day_count++;   
$end_days++;
}   
echo "</tr></table>";
?>
</body>
</html>