首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何筛选表格,然后以pdf格式打印输出

如何筛选表格,然后以pdf格式打印输出
EN

Stack Overflow用户
提问于 2018-08-08 03:29:31
回答 1查看 1K关注 0票数 -3

大家好,我正试着过滤一个表格,然后将过滤后的表格打印成pdf格式。

我输入了其他代码,但这是我能完成的最接近的代码。

我尝试了不同的解决方案,但似乎不能解决它。

如果你们对如何做到这一点有任何建议,尽管说。任何帮助都是最好的。

帮帮大伙!提前感谢!

这是reportss.php

<?php 
ob_start(); 
include ('filters.php');
  function fetch_data()  
  {  
  require('filters.php');
  $output = '';  
  $conn = mysqli_connect("localhost", "root", "", "trans");  
  $sql = "SELECT * FROM customers ORDER BY created_at ASC";  
  $result = mysqli_query($conn, $sql);  
  while($row = mysqli_fetch_array($result))  
  {       
  $output .= '<tr>  
                      <td>'.$row["id"].'</td>  
                      <td>'.$row["first_name"].'</td>  
                      <td>'.$row["last_name"].'</td>  
                       <td>'.$row["email"].'</td>  
                      <td>'.$row["created_at"].'</td>  
                 </tr>  
                      ';  
  }  
  return $output;  
  }    if(isset($_POST["generate_pdf"]))  
 {  
  require_once('tcpdf/tcpdf.php');  
  $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', 
  false);  
  $obj_pdf->SetCreator(PDF_CREATOR);  
  $obj_pdf->SetTitle("Customer Transactions Report");  
  $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
  $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', 
  PDF_FONT_SIZE_MAIN));  
  $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', 
  PDF_FONT_SIZE_DATA));  
  $obj_pdf->SetDefaultMonospacedFont('helvetica');  
  $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
  $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);  
  $obj_pdf->setPrintHeader(false);  
  $obj_pdf->setPrintFooter(false);  
  $obj_pdf->SetAutoPageBreak(TRUE, 10);  
  $obj_pdf->SetFont('helvetica', '', 11);  
  $obj_pdf->AddPage();  
  $content = '';  
  $content .= '  
  <h4 align="center">Customer Transactions Report for Viaje</h4><br /> 
  <table border="1" cellspacing="0" cellpadding="3">  
       <tr>  
            <th width="26%">ID</th>  
            <th width="15%">First Name</th>  
            <th width="15%">Last Name</th>  
            <th width="33%">Email</th>  
            <th width="13%">Order Date</th>  
       </tr>  
  ';  
  $content .= fetch_data();  
  $content .= '</table>';  
  $obj_pdf->writeHTML($content);  
  $obj_pdf->Output('file.pdf', 'I');  
  exit();

 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
  <head>  

       <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">Showings Customer Transactions</h2>  <br><br><br><BR><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-5">  
                <form method="post">  
                  <input type="button" name="filter" id="filter" value="Filter" class="btn btn-info" />  

                  <input type="submit" name="generate_pdf" class="btn btn-success" value="Print" />  
                </form>
            </div>  
            <div style="clear:both"></div>                 
            <br />  
            <div id="order_table">  

                 <table class="table table-bordered">  
                      <tr>  
                           <th width="5%" >ID</th>  
                           <th width="30%" >First Name</th>  
                           <th width="43%" >Last Name</th>  
                           <th width="10%" >Email</th>  
                           <th width="12%" >Order Date</th>  
                      </tr>
                <?php  
                 echo fetch_data();  
                 ?>  



                 </table>  

            </div>  
       </div>  

  </body>  

  <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:"filters.php",  
                      method:"POST",  
                      data:{from_date:from_date, to_date:to_date},  
                      success:function(data)  
                      {  
                           $( '#order_table').html(data);  
                      }  
                 });  
            }  
            else  
            {  
                 alert("Please Select Date");  
            }  
       });  
  });  

这是filters.php

 <?php  

 if(isset($_POST["from_date"], $_POST["to_date"]))  
 {  
  $connect = mysqli_connect("localhost", "root", "", "trans");  
  $output = '';  

  $query = "  
       SELECT * FROM customers  
       WHERE cast(created_at as date) 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%">First Name</th>  
                 <th width="43%">Last Name</th>  
                 <th width="10%">Email</th>  
                 <th width="12%">Order Date</th>  
            </tr>  
  ';  
  if(mysqli_num_rows($result) > 0)  
  {  
       while($row = mysqli_fetch_array($result))  
       {  
            $output .= '  
                 <tr>  
                      <td>'. $row["id"] .'</td>  
                      <td>'. $row["first_name"] .'</td>  
                      <td>'. $row["last_name"] .'</td>  
                      <td>$ '. $row["email"] .'</td>  
                      <td>'. $row["created_at"] .'</td>  
                 </tr>  
            ';  
       }  
  }  
  else  
  {  
       $output .= '  
            <tr>  
                 <td colspan="5">No Order Found</td>  
            </tr>  
       ';  
  }  
  $output .= '</table>';  
  echo $output;  
 }  
 ?>
EN

回答 1

Stack Overflow用户

发布于 2018-08-08 04:08:16

您尝试使用的$_POST变量只有在您发送帖子本身之后才会被定义。当您第一次加载此页面时,其中没有任何内容,因此,query()将不存在,因此,您将看到此错误。

在这种情况下,您应该强制在查询中使用日期,以防表单尚未发送任何内容。

截止日期:

$fromDate = isset($_POST["from_date"]) ? $_POST["from_date"] : date('Y-m-d');
$untilDate = isset($_POST["to_date"]) ? $_POST["to_date"] : date('Y-m-d', strtotime('+5 days'));

if (isset($fromDate, $untilDate)) {...}

然而,正如每个人都指出的那样,使用全局变量不是推荐的

我还建议你读一读这篇文章:几年前,https://www.phptherightway.com/帮了我很多。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51734122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档