按日期范围和关键字搜索

时间:2018-07-20 07:56:44

标签: php sql ajax

为了使项目中的报告生成,我想使用2种方法:

  1. 按日期范围(从日期到最新)
  2. 通过员工编号

使用上述关键字时,应在该特定范围内显示员工的完整记录。

例如,exemp1234是要在2018年5月20日至2018年6月30日期间检查其记录的员工。结果必须显示在此范围内

另一件事是,可以按日期或按员工ID或按两者进行搜索,但这两者都不是强制性的。

脚本如下:

<script>  
      $(document).ready(function(){  
           $.datepicker.setDefaults({  
                dateFormat: 'yy-mm-dd'   
           });  
           $(function(){  
                $("#from_date").datepicker();  
                $("#to_date").datepicker();  
           });  
           $('#filter').click(function(){  
                var from_date = $('#from_date').val();  
                var to_date = $('#to_date').val();  
                if(from_date != '' && to_date != '')  
                {  
                     $.ajax({  
                          url:"filter.php",  
                          method:"POST",  
                          data:{from_date:from_date, to_date:to_date},  
                          success:function(data)  
                          {  
                               $('#report_table').html(data);  
                          }  
                     });  
                }  
                else  
                {  
                     alert("Please Select Date");  
                }  
           });  
      });  
 </script>

<script>
$(document).ready(function(){

 load_data();

 function load_data(query)
 {
  $.ajax({
   url:"fetch.php",
   method:"POST",
   data:{query:query},
   success:function(data)
   {
    $('#report_table').html(data);
   }
  });
 }
 $('#search_text').keyup(function(){
  var search = $(this).val();
  if(search != '')
  {
   load_data(search);
  }
  else
  {
   load_data();
  }
 });
});
</script>

我需要将这两个不同的查询合并为一个。

filter.php

<?php  
 //filter.php  
 if(isset($_POST["from_date"], $_POST["to_date"]))  
 {  
      $connect = mysqli_connect("localhost", "root", "", "ais");  
      $output = '';  
      $query = "  
           SELECT * FROM tbl_employees,tbl_in_time_attendance,tbl_out_time_attendance  WHERE tbl_employees.EmpId=tbl_in_time_attendance.EmpId AND tbl_in_time_attendance.EmpId=tbl_out_time_attendance.EmpId AND tbl_in_time_attendance.date=tbl_out_time_attendance.date AND tbl_in_time_attendance.date AND tbl_out_time_attendance.date  BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]."'  
      ";  
      $result = mysqli_query($connect, $query); 
      $cnt=1;     
      $output .= '  
           <table class="table table-bordered">  
                <tr>  
                     <th width="6%">Sr No.</th>  
                     <th width="15%">EMPLOYEE ID</th> 
                     <th width="20%">EMPLOYEE NAME</th>
                     <th width="15%">IN_TIME</th>  
                     <th width="15%">OUT_TIME</th>  
                     <th width="15%">DATE</th> 
                     <th width="25%">WORKING HOURS</th> 
                </tr>  
      ';  
      if(mysqli_num_rows($result) > 0)  
      {  
           while($row = mysqli_fetch_array($result))  
           {  
                        $outtime = $row["outtime"];
                        $intime = $row["intime"];
                        $array1 = explode(':', $intime);
                        $array2 = explode(':', $outtime);

                        $minutes1 = ($array1[0] * 60.0 + $array1[1])/60;
                        $minutes2 = ($array2[0] * 60.0 + $array2[1])/60;

                        $diff = round(($minutes2 - $minutes1),1).' hrs';


                $output .= '  
                     <tr>  
                          <td align="center">'. $cnt .'</td>  
                          <td align="center">'. $row["EmpId"] .'</td>  
                          <td align="center">'. $row["FirstName"] . " " . $row["LastName"].'</td> 
                          <td align="center">'. $row["intime"] .'</td>  
                          <td align="center"> '. $row["outtime"] .'</td>  
                          <td align="center">'. $row["date"] .'</td> ';
        if ($diff < 8) {
        $output .= '<td align="center" style="background-color: #F42E2E;"> ' . $diff . ' </td>';
} else {
    $output .= '<td align="center" style="background-color: #55E443;"> ' . $diff . ' </td>';
}
$output .= '</tr>';

          $cnt++;
           } 

      }  
      else  
      {  
           $output .= '  
                <tr>  
                     <td colspan="5">No Record Found</td>  
                </tr>  
           ';  
      }  
      $output .= '</table>';  
      echo $output;  
 }  


 ?>

Fetch.php

<?php

$connect = mysqli_connect("localhost", "root", "", "ais");
$output = '';
if(isset($_POST["query"]))
{

 $query = "
          SELECT * FROM tbl_employees,tbl_in_time_attendance,tbl_out_time_attendance  WHERE tbl_employees.EmpId=tbl_in_time_attendance.EmpId AND tbl_in_time_attendance.EmpId=tbl_out_time_attendance.EmpId AND tbl_in_time_attendance.date=tbl_out_time_attendance.date AND tbl_in_time_attendance.date AND tbl_employees.EmpId LIKE '".$_POST["query"]."' ";
}
else
{
 $query = "SELECT * from tbl_employees, tbl_in_time_attendance , tbl_out_time_attendance WHERE tbl_employees.EmpId=tbl_in_time_attendance.EmpId AND tbl_in_time_attendance.EmpId=tbl_out_time_attendance.EmpId AND tbl_in_time_attendance.date=tbl_out_time_attendance.date";  

}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
 $cnt=1;

 $output .= '

   <table class="table table-bordered">  
                <tr>  
                     <th width="6%">Sr No.</th>  
                     <th width="15%">EMPLOYEE ID</th> 
                     <th width="20%">EMPLOYEE NAME</th>
                     <th width="15%">IN_TIME</th>  
                     <th width="15%">OUT_TIME</th>  
                     <th width="15%">DATE</th> 
                     <th width="25%">WORKING HOURS</th> 
                </tr>  
 ';
 while($row = mysqli_fetch_array($result))
 {
  $outtime = $row["outtime"];
                        $intime = $row["intime"];
                        $array1 = explode(':', $intime);
                        $array2 = explode(':', $outtime);

                        $minutes1 = ($array1[0] * 60.0 + $array1[1])/60;
                        $minutes2 = ($array2[0] * 60.0 + $array2[1])/60;

                        $diff = round(($minutes2 - $minutes1),1).' hrs';


                $output .= '  
                     <tr>  
                          <td align="center">'. $cnt .'</td>  
                          <td align="center">'. $row["EmpId"] .'</td>  
                          <td align="center">'. $row["FirstName"] . " " . $row["LastName"].'</td> 
                          <td align="center">'. $row["intime"] .'</td>  
                          <td align="center"> '. $row["outtime"] .'</td>  
                          <td align="center">'. $row["date"] .'</td> ';
        if ($diff < 8) {
        $output .= '<td align="center" style="background-color: #F42E2E;"> ' . $diff . ' </td>';
} else {
    $output .= '<td align="center" style="background-color: #55E443;"> ' . $diff . ' </td>';
}
$output .= '</tr>';

          $cnt++;
 }
 }
 else  
      {  
           $output .= '  
                <tr>  
                     <td colspan="5">No Record Found</td>  
                </tr>  
           ';  
      }  
      $output .= '</table>';  
      echo $output;  




?>

我希望这些文件将提取和过滤文件分离为一个文件。 请帮忙。

1 个答案:

答案 0 :(得分:0)

请不要再删除问题,我正在写几分钟,然后问题被删除了哈哈

soo,因为必须单击过滤器按钮才能获得结果,所以有两种选择方法!

  1. 您将使用datatable插件显示表,这意味着您只需要按日期过滤,然后在搜索框中输入employeeid,您将只看到该人员的结果,而无需再次查询! https://datatables.net/

  2. 您将获得两个日期和搜索查询,方法是单击“筛选器”按钮,这意味着您将只有一个php文件(我不知道为什么您实际上要使用访存和筛选器)。.在您的php文件中然后查看是否只有提交的日期或是否也提交了查询:

    $('#filter').click(function(){  
       var from_date = $('#from_date').val();  
       var to_date = $('#to_date').val();  
       var query = $("#search_text").val();
    
       if(from_date != '' && to_date != '')  
       {  
         $.ajax({  
              url:"filter.php",  
              method:"POST",  
              data:{from_date:from_date, to_date:to_date, employee: query},  
              success:function(data)  
              {  
                   $('#report_table').html(data);  
              }  
         });  
       }  
       else  
       {  
         alert("Please Select Date");  
       }  
    });  
    

在php文件中

if(isset($_POST["employee"]) AND $_POST["employee"] != "") {
    // make your sql call with dates AND employeeid
} else {
    // make your sql call only with dates
}

根据您获得的结果数,我确定也可以不必再次查询不同的日期,而是调整数据表,以便您拥有一个from_date,一个to_date和一个搜索字段,并在键入键后过滤表..参见示例:https://datatables.net/examples/api/multi_filter.html