按下拉列表过滤表并将日期/导出过滤到Excel

时间:2018-01-14 20:14:29

标签: php ajax excel filter export

下午好,Team,

我一直在寻找互联网/ stackoverflow很长一段时间了,我无法解决一个困扰我一段时间的问题。想要避免它(你不想看到我的浏览器历史记录)。

问题是,这个问题导致了另一个问题,也许你可能会提供一些建议。

在TL之前; DR - 这是问题:

成分: 1. 1个数据库(phpmyadmin) 2. 1表 3. 3个文件(filter.html / raport.php / export.php)

导致另一个问题:

我想要做的是,我希望按日期范围(有效)和下拉列表(不起作用)过滤表格中的结果。

请参阅filter.html中的代码:

    <html>  
      <head>  
           <title> Raport</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>  
           <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">  
      </head>  
      <body>  

           <br /><br />  
           <div class="container" style="width:900px;">  
                <h2 align="center"> Raport</h2>  
                <h3 align="center">Order Data</h3><br />  

                <div class="col-md-3">  
                 <input type="text" name="from_date" id="from_date" class="form-control" placeholder="From Date" />  
                </div>  
                <div class="col-md-3">  
                  <input type="text" name="to_date" id="to_date" class="form-control" placeholder="To Date" />  
                </div>  
                <div class="col-md-4">  
                    <select  type="text" name="team" id="team" class="form-control  ">
                        <option value="">Select team</option>
                        <option value="pt">pt</option>
                        <option value="it">it</option>
                    </select> 
                </div>

                <div class="col-md-4">  
                     <input type="button" name="filter" id="filter" value="Filter" class="btn btn-info" />  
                </div>  

                <form method="post" action="export.php" id="export_form">
                    <input type="hidden" name="from_date" id="from_date1" class="form-control"  />  
                    <input type="hidden" name="to_date" id="to_date1" class="form-control"  />  
                    <input type="submit" name="export" id="export_btn" class="btn btn-success" value="Export" />
                </form>
                <div style="clear:both"></div>                 
                <br />  
                <div id="order_table">  
                     <table class="table table-bordered">  
                          <tr>  
                               <th width="5%">Id</th>  
                               <th width="30%">name1</th>  
                               <th width="43%">name2</th>  
                               <th width="43%">name3</th>  
                               <th width="10%">name4</th>  
                               <th width="12%">name5</th>  
                               <th width="12%">name6</th>  
                               <th width="12%">name7</th>  
                               <th width="12%">name8</th>  
                          </tr>  
                     <?php  
                     while($row = mysqli_fetch_array($result))  
                     {  
                     ?>  
                          <tr>
                    <td><?php echo $row['id'];?></td>
                    <td><?php echo $row['name1'];?></td>
                    <td><?php echo $row['name2'];?></td>
                    <td><?php echo $row['name3'];?></td>
                    <td><?php echo $row['name4'];?></td>
                    <td><?php echo $row['name5'];?></td>
                     <td><?php echo $row['name6'];?></td>
                     <td><?php echo $row['name7'];?></td>
                     <td><?php echo $row['name8'];?></td>

                </tr> 
                     <?php  
                     }  
                     ?>  
                     </table>  

                </div>  
           </div>  

      </body>  
 </html>  
 <script>  
      $(document).ready(function(){

           $.datepicker.setDefaults({  
                dateFormat: 'yy-mm-dd'   
           });  
           $(function(){  
                $("#from_date").datepicker({
                        onSelect: function(dateText, inst){
                            $('#from_date1').val(dateText);
                        }
                });  
                $("#to_date").datepicker({
                        onSelect: function(dateText, inst){
                            $('#to_date1').val(dateText);
                        }
                });  
           });  

           if($("#from_date").val() != ''){
                $('#from_date1').val($("#from_date").val());
           }
           if($("#to_date").val() != ''){
                $('#to_date1').val($("#to_date").val());
           }
           $('#filter').click(function(){  
                var from_date = $('#from_date').val();  
                var to_date = $('#to_date').val();  
                if(from_date != '' && to_date != '')  
                {  
                     $.ajax({  
                          url:"raport.php",  
                          method:"POST",  
                          data:{from_date:from_date, to_date:to_date},  
                          success:function(data)  
                          {  
                               $('#order_table').html(data);  
                          }  
                     });  
                }  
                else  
                {  
                     alert("Please Select Date");  
                }  
           });  
      });  
 </script>

请参阅raport.php中的代码:

<?php  

 if(isset($_POST["from_date"], $_POST["to_date"]))  
 {  
      $connect = mysqli_connect("localhost", "root", "", "database");  
      $output = '';  
      $query = "  
           SELECT * FROM fp_data 
              WHERE submitdate BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]."'     
      ";  
      $result = mysqli_query($connect, $query);  
      $output .= '  
           <table class="table table-bordered">  
                <tr>  
                      <th width="5%">Id</th>  
                               <th width="30%">name1</th>  
                               <th width="43%">name2</th>  
                               <th width="43%">name3</th>  
                               <th width="10%">name4</th>  
                               <th width="12%">name5</th>  
                               <th width="12%">name6</th>  
                               <th width="12%">name7</th>  
                               <th width="12%">name8</th>  
                </tr>  
      ';  
      if(mysqli_num_rows($result) > 0)  
      {  
           while($row = mysqli_fetch_array($result))  
           {  
                $output .= '  
                     <tr>  
                          <td>'.$row["id"].'</td>  
         <td>'.$row["name1"].'</td>  
         <td>'.$row["name2"].'</td> 
         <td>'.$row["name3"].'</td>  
         <td>'.$row["name4"].'</td>
         <td>'.$row["name5"].'</td>  
         <td>'.$row["name6"].'</td>  
         <td>'.$row["name7"].'</td>  
         <td>'.$row["name8"].'</td>  

                     </tr>  
                ';  
           }  
      }  
      else  
      {  
           $output .= '  
                <tr>  
                     <td colspan="12">No data found</td>  
                </tr>  
           ';  
      }  
      $output .= '</table>';  
      echo $output;  
 }  
 ?>

请参阅export.php中的代码:

  <?php  
//export.php  
$connect = mysqli_connect("localhost", "root", "", "database");
$output = '';
if(isset($_POST["export"]))
{
 $from_date = $_POST["from_date"];
 if(!empty($from_date)) {
    $from_date = $from_date." 00:00:00";
 } else{
    $from_date = date("Y-m-d")." 00:00:00"; 
 }

 $to_date = $_POST["to_date"];
 if(!empty($to_date)) {
    $to_date = $to_date." 23:59:59";
 } else {
    $to_date = date("Y-m-d")." 23:59:59";
 }


 $query = "SELECT * FROM fp_data WHERE submitdate >= '$from_date' AND submitdate <= '$to_date'";

 $result = mysqli_query($connect, $query);

 if(mysqli_num_rows($result) > 0)
 {
  $output .= '
   <table class="table" bordered="1" style="border: 1px solid #000">  
 <tr>  
       <th>id</th>  
       <th>name1</th>  
       <th>name2</th>  
       <th>name3</th>
       <th>name4</th>
       <th>name5</th>
       <th>name6</th>  
       <th>name7</th>  
       <th>name8</th>
                    </tr>
  ';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '
    <tr>  
      <td>'.$row["id"].'</td>  
         <td>'.$row["name1"].'</td>  
         <td>'.$row["name2"].'</td> 
         <td>'.$row["name3"].'</td>  
         <td>'.$row["name4"].'</td>
         <td>'.$row["name5"].'</td>  
         <td>'.$row["name6"].'</td>  
         <td>'.$row["name7"].'</td>  
         <td>'.$row["name8"].'</td>  

                    </tr>
   ';
  }
  $output .= '</table>';
  header('Content-Type: application/xls');
  header('Content-Disposition: attachment; filename=raportlte.xls');
  echo $output;
 }
}
?>

另一个问题是我如何导出它?

总而言之,我希望按日期范围和下拉列表过滤数据,然后将表格中的内容导出为Excel。

任何帮助都是相关的。

0 个答案:

没有答案