在php中过滤动态数据表

时间:2016-08-05 05:00:59

标签: php mysql

enter image description here

我的第一个网站是用php开发的,用户可以显示员工生物指标日志,他们可以通过在组合列表中选择来操纵表格应该显示在哪个部门,也可以通过excel导出。现在我想使用日期2时间选择器过滤我的数据与日期范围,有人可以给我一些链接或示例,可以很容易地了解像我这样的新手。下面是我的Index.php

的index.php

    <?php
      $conn =mysqli_connect("localhost", "root", "", "bio_db");

      function filterTable($sql)
       {
        $connect = mysqli_connect("localhost", "root", "","bio_db");
        $filter_Result = mysqli_query($connect, $sql);
        return $filter_Result;
      }

      $post_at = "";
      $post_at_to_date = "";
      $queryCondition = "";

    if(isset($_POST["search"]["post_at"])) {
    $post_at = $_POST["search"]["post_at"];
    list($fid,$fim,$fiy) = explode("-",$post_at);


    $post_at_to_date = date('Y-m-d');
    if(isset($_POST["search"]["post_at_to_date"])) {
        $post_at_to_date = $_POST["search"]["post_at_to_date"];
        list($tiy,$tim,$tid) = explode("-",$_POST["search"] ["post_at_to_date"]);
        $post_at_to_date = "$tiy-$tim-$tid";
    }

        $queryCondition .= "Where post_at BEETWEEN '$fiy-$fim-$fid' AND '" .$post_at_to_date. "'";
}

     else { $sql = "SELECT * from daily_data2 " .$queryCondition. " ORDER BY post_at ASC";
        $search_result = filterTable($sql);
    }


    ?>



  <html>
   <head>
   <title>Time and Attendance Monitoring</title>        
   <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
   <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

   <style>
    .table-content{border-top:#CCCCCC 4px solid; width:50%;}
    .table-content th {padding:5px 20px; background: #F0F0F0;vertical-align:top;} 
    .table-content td {padding:5px 20px; border-bottom: #F0F0F0 1px     solid;vertical-align:top;} 
    </style>
    </head>

    <body>
    <div class="demo-content">
    <h2 class="title_with_link">Time and Attendance Monitoring</h2>
   <form name="frmSearch" method="post" action="index.php">
    <p class="search_input">
    <input type="text" placeholder="From Date" id="post_at" name="search[post_at]"  value="<?php echo $post_at; ?>" class="input-control" />
    <input type="text" placeholder="To Date" id="post_at_to_date" name="search[post_at_to_date]" style="margin-left:10px"  value="<?php echo $post_at_to_date; ?>" class="input-control"  />             
    <input type="submit" name="go" value="Search" >
    </p>
   <?php if(!empty($result))     { ?>



  <table align="center" width="600" border="1" cellpadding="1" cellspacing="1">
        <tr>
            <th>Userid</th>
            <th>Name</th>
            <th>Campaign</th>
            <th>Date</th>
            <th>Hoursworked</th>
            <th>Overtime</th>
        </tr>

    <?php while($row = mysqli_fetch_array($search_result)):?>
        <tr>
            <td style="text-align:center;"><?php echo $row['Userid'];?></td>
            <td width="200"><?php echo $row['Name'];?></td>
            <td style="text-align:center;"><?php echo $row['Campaign'];?>       </td>
            <td width="100" style="text-align:center;"><?php echo    $row['Date'];?></td>
            <td style="text-align:center;"><?php echo $row['Hoursworked'];?></td>
            <td style="text-align:center;"><?php echo $row['Overtime'];?>   </td>
        </tr>

    <?php endwhile;?>

        </table>
   <?php } ?>
   </form>
   </div>
   <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
   <script>
   $.datepicker.setDefaults({
    showOn: "button",
    buttonImage: "datepicker.png",
    buttonText: "Date Picker",
    buttonImageOnly: true,
    dateFormat: 'yy-mm-dd'  
   });
    $(function() {
    $("#post_at").datepicker();
    $("#post_at_to_date").datepicker();
    });
   </script>
   </body>
  </html>

1 个答案:

答案 0 :(得分:1)

我仍然不明白你的问题,但我想你是在问这个问题,

$query  = "SELECT * FROM daily_data2 WHERE (DATE BETWEEN 'YOUR-FROM-DATE'  AND 'YOUR-TO-DATE')";

在查询中提供您的FROM日期和TO日期,这将为您提供这两个日期之间的数据,如DestinatioN所述

  

修改

使用,"'$post_at_to_date'"代替'" .$post_at_to_date. "'(在'AND'条件之后),因为它只是打印变量名。