dongxun2089 2015-01-31 08:27
浏览 22
已采纳

如何根据月份在两个html表中并排显示日期?

I am trying to display status of employee for each date in a month in the given application. i have divided data in two tables. Snapshot of application

Now, if you can see , My tables are displaying dates correctly.

But i want to start new table for each month. i.e. whenever 1st date of new month begins, i want to start the procedure of rendering dates for the month again.

So i will be able to distinguish them by their months.

I hope i have explained the problem correctly. Please provide guidance on this . Thanks in advance. My code for the given problem :

<?php       
    $name=$_SESSION['sess_username'];
    $dateofattendance=$_SESSION['sess_date'];
    $time="00-00-00";
    $status="absent";
    $counter=0;
    $conn = new mysqli('localhost', '', '', 'test');
    $sql="SELECT dateofattendance,timeofattendance, status,timeofdeparture FROM attendance Where emp='$name' ORDER BY dateofattendance ASC ";
    $result = $conn->query($sql);           
    if ($result->num_rows > 0)         
        {
            echo "<table class='main'><tbody><tr><td >";
            // create the opening table
            echo "<div align='left'><table class='sep1'style='float:left;'; border='black' cellpadding='5' ><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead>tbody>";
            while($row = $result->fetch_assoc())
                {
                  // create the row
                  echo "<tr><td>" . $row["dateofattendance"]. "</td><td>" . $row["timeofattendance"]. "</td><td>" . $row["timeofdeparture"]. "</td>";
                  if($row["status"]=='present')
                      {
                          echo "<td ><span class='label label-success'>". $row["status"]."</span></td>";
                      }
                  else
                      {
                          echo "<td><span class='label label-danger'>". $row["status"]."</span></td>"; 
                     }"
                </tr>";
            $counter++;  

           // when the counter is 15, close this table and open another
           if($counter == 15)
           {
               echo "</td><td>"; // move to the next cell in our wrap table
               echo "</tbody></table></div>&nbsp;&nbsp;";
               echo "<table class='sep2'style='float:left;'border='black'cellpadding='5'><thead> <tr><th> Date </th><th>IN</th><th>OUT</th><th>Status</th></tr></thead><tbody>";  
           } 

         }
// close the last table
echo "</tbody></table>";

// close our wrapper table
echo "</td></tr></tbody></table>";
}

        $conn->close();
?>
  • 写回答

1条回答 默认 最新

  • du21064 2015-01-31 11:35
    关注

    Instead of having a counter and creating a new table when it hits 15, you want to have a variable that stores the month number of the last row rendered, and check if the month number of the next row to be rendered matches. If it doesn't match, generate a new table.

    The following code is produces the results you want. I've tested it with a database table with identical structure (table named "test_employees").

    Note, the code outputs valid HTML and I re-formatted the echo statements and added newline chars to make the both the PHP and HTML source code more readable.

    This code could be cleaned up considerably, so it's by no means good production code, but again, it demonstrates the logic you are after.

    One item to note, the code only works within a given year. Additional code would be required if you wanted to display tables across multiple years.

    If this helps you, please mark my answer.

    I can also spend more time to refactor this code if you want. Let me know and I'll reply faster the next time.

    <!DOCTYPE html>
    <html>
    <head>
    <title>table test</title>
    </head>
    <body>
    
    <?php
        $name=$_SESSION['sess_username'];
        $dateofattendance=$_SESSION['sess_date'];
        $time="00-00-00";
        $status="absent";
        $counter=0;
        $conn = new mysqli('localhost', '', '', 'test_employees');
        $sql="SELECT dateofattendance,timeofattendance, status,timeofdeparture FROM attendance Where emp='$name' ORDER BY dateofattendance ASC ";
        $result = $conn->query($sql);
    
        if ($result->num_rows > 0)         
        {
            echo "<table border='1'>";
            echo "<thead>";
            echo "<tr>";
            echo "<th>Date</th>";
            echo "<th>IN</th>";
            echo "<th>OUT</th>";
            echo "<th>Status</th>";
            echo "</tr>";
            echo "</thead>";
            echo "<tbody>";
    
            $first_iteration = 1;
            $row = $result->fetch_array(MYSQLI_ASSOC);
            $last_month_num = split('-', $row["dateofattendance"])[1];
    
            while($row = $result->fetch_assoc())
            {
                $current_month_num = split('-', $row["dateofattendance"])[1];
    
                if ($first_iteration == 1) { // do nothing the first time around
                    $first_iteration = 0;
                }
                else if ($current_month_num == $last_month_num) { // only close row in current table
                    echo "</tr>";
                }
                else { // close table and start new table
                    echo "</tr>";
                    echo "</table>";
                    echo "<table border='1'>";
                    echo "<thead>";
                    echo "<tr>";
                    echo "<th>Date</th>";
                    echo "<th>IN</th>";
                    echo "<th>OUT</th>";
                    echo "<th>Status</th>";
                    echo "</tr>";
                    echo "</thead>";
                    echo "<tbody>";
                }
    
                echo "<tr>" . "
    ";
    
                echo "<td>" . "
    " . $row["name"] . $row["dateofattendance"] . "
    " . "</td>" . "
    ";
                echo "<td>" . "
    " . $row["timeofattendance"] . "
    " . "</td>" . "
    ";
                echo "<td>" . "
    " . $row["timeofdeparture"] . "
    " . "</td>" . "
    ";
    
                if($row["status"]=='present')
                {
                    echo "<td><span class='label label-success'>". $row["status"]."</span></td>";
                }
                else
                {
                    echo "<td><span class='label label-danger'>". $row["status"]."</span></td>";
                }
    
                $counter++;
    
                $last_month_num = split('-', $row["dateofattendance"])[1];
            }
    
            echo "</tr>";
            echo "</table>";
        }
    
    
        $conn->close();
    ?>
    
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面