dongle3217 2015-03-15 02:08
浏览 61
已采纳

如何在html表(日历)中一起添加行值?

I have a calendar I made with php using an html table.

Each date has an integer value inserted from the database below the day number.

What I want to do is get the sum of each row (week) of the table and put it in the 8th column. How can I do this?

Any help?

    <?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>

The above code outputs this:

Calendar

  • 写回答

1条回答 默认 最新

  • duanfu4446 2015-03-15 03:45
    关注

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

    Try this code:

         <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择