使用Table,使用PHP在MySQL中按日期分隔数据

时间:2016-08-01 07:15:38

标签: php mysql database html-table

这是来自PhpMyadmin数据: enter image description here

这些是代码。

<table class="table table-bordered">
  <thead>
    <tr><th>Sn</th><th>Status</th> <th>Location</th> <th>Time</th></tr>
  </thead>
  <tbody>
    <?php
        $i=0;
        $query2 = "SELECT * FROM tbl_statustimeline WHERE cons_no = '$cons_no' ORDER BY date DESC";
        $results2 = $mysqli->query($query2);
        if($results2){
            while($row2 = $results2->fetch_assoc()) {
                $i++;
    ?>
    <tr>
      <td colspan='4'><?php echo $row2['date']; ?></td>
    </tr>
    <tr>
        <td><?php echo $i; ?></td>
        <td><?php echo $row2['status']; ?> - <?php echo $row2['Location']; ?></td>
        <td><?php echo $row2['address']; ?></td>
        <td><?php echo $row2['time']; ?></td>
    </tr>
    <?php
          }
        }
    ?>
         </tbody>
    </table>

所以我需要2016-07-12,2016-07-15,2016-07-19等日期与这些信息分开显示。

2 个答案:

答案 0 :(得分:1)

如果您想获得与DHL跟踪系统相同的结果,您有两种变体:

    <script type="text/javascript">
    $(document).ready(
        function() {
            $("#search").keyup(
                    function () 
                    {
                        var value = this.value.toLowerCase().trim();
                        $("table tr").each(
                            function (index) 
                            {
                                if (!index) return;
                                $(this).find("td").children().attr("value").each(
                                     function () 
                                     {
                                         var id = $(this).text().toLowerCase().trim();
                                         var not_found = (id.indexOf(value) == -1);
                                         $(this).closest('tr').toggle(!not_found);
                                         return not_found;
                                     });
                            });
                    }); 
        });
    </script>

   <!-- Fetch table data -->
   <sql:setDataSource var="myDataSource" driver="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@10.1.56.49:1521:something" user="something" password="something"/>
   <sql:query var="result" sql="select * from garageproducts" dataSource="${myDataSource}"/>

    <table id="mytable" class="table table-hover">
            <thead>
              <tr>
                <th>Mark</th>
                <th>Barcode</th>
                <th>Name</th>
                <th>Brand</th>
                <th>Stock</th>
                <th>Cost</th>
              </tr>
            </thead>
            <tbody>
                <%! int cnt=0; %>
                <c:forEach var="row" items="${result.rows}">
                  <tr>
                    <td hidden="true">${row.pid}</td>
                    <td>
                        <input type="checkbox" value="">
                    </td>
                    <td><input type="text" id='<%= "barcode"+cnt %>' value="${row.barcode}" name="barcodename" class="form-control input-sm"></td>
                    <td><input type="text" id='<%= "name"+cnt %>' value="${row.name}" name="namename" class="form-control input-sm"></td>
                    <td><input type="text" id='<%= "brand"+cnt %>' value="${row.brand}" name="brandname" class="form-control input-sm"></td>
                    <td><input type="text" id='<%= "stock"+cnt %>' value="${row.stock}" name="stockname" class="form-control input-sm"></td>
                    <td><input type="text" id='<%= "cost"+cnt %>' value="${row.cost}" name="costname" class="form-control input-sm"></td>
                  </tr>
                  <% ++cnt; %>
                </c:forEach>

            </tbody>
    </table>

然后在脚本中检查当前行中的日期是否与上一行中的日期不同 - 这是您必须显示日期分隔符的时刻;

1) SELECT * FROM table ORDER BY TIMESTAMP(date_column,time_column) DESC

您首先使用上述查询收集所需的所有日期,然后对于每个日期,您必须显示日期分隔符并发出另一个SQL查询

2) SELECT DISTINCT date_column FROM table

获取时间和状态

答案 1 :(得分:1)

<table class="table table-bordered">
      <thead>
        <tr><th>Sn</th><th>Status</th> <th>Location</th> <th>Time</th></tr>
      </thead>
      <tbody>
        <?php
            $i=0;
            $cons_no=$row["cons_no"];
            $query2 = "SELECT * FROM tbl_statustimeline WHERE cons_no = '$cons_no' ORDER BY date DESC";
            $results2 = $mysqli->query($query2);
            if($results2){
                $currentDate = false;
                while($row2 = $results2->fetch_assoc()) {
                    $i++;
                    if ($row2['date'] != $currentDate){
        ?>
        <tr>
          <td colspan='4'><?php echo $row2['date']; ?></td>
        </tr>
        <?php $currentDate = $row2['date'];
            }
        ?>
        <tr>
            <td><?php echo $i; ?></td>
            <td><?php echo $row2['status']; ?> - <?php echo $row2['Location']; ?></td>
            <td><?php echo $row2['address']; ?></td>
            <td><?php echo $row2['time']; ?></td>
        </tr>
        <?php
              }
            }
        ?>
        </tbody>
  </table>

我的出局就像这张照片。 enter image description here

相关问题