嗨,我正在尝试过滤表格,然后将过滤后的表格打印为pdf。
我输入了其他代码,但这已经接近完成了。
我尝试了其他解决方案,但似乎无法修复。
如果您对如何操作有任何建议,请说。任何帮助都会很棒。
帮助大家!预先感谢!
这是reports.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;
}
?>
答案 0 :(得分:2)
您要使用的$_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/ 几年前对我有很大帮助。