【发布时间】:2019-01-14 23:27:10
【问题描述】:
大家好,我正在尝试过滤表格,然后将过滤后的表格打印为 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;
}
?>
【问题讨论】:
-
filterss.php是您的问题文本中的错字吗?因为在您的代码中包含filters.php并且在该文件中您有注释//filter.php -
是名为filters.php(如包含)或filters.php(如文中给出)的文件?
-
@James 抱歉忘记删除评论及其只是 filters.php
-
@Mandyshaw 抱歉忘记删除评论及其只是 filters.php
-
如果您刚刚开始使用 PHP 并想要构建应用程序,我还强烈建议您查看各种 development frameworks 以查看是否可以找到适合您的风格和需求的。它们有多种风格,从 Fat-Free Framework 这样的轻量级到像 Laravel 这样的更全面。这些为您提供了具体的示例,以及关于如何编写代码和组织文件的更有力的指导。你这里的代码很混乱。