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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?