我有以下 php 表,如何将打印功能添加到 php 表中?还有一个按钮,单击时,通过打印机打印下表,我尝试了"CTRL + P",我只得到了页面示例的html部分,页眉,页脚,导航栏,而不是结果php结果
<?php
echo "<table border='1' id= results>
<tr>
<th>FILEID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Issue Date</th>
<th>Interest Rate</th>
<th>Terms</th>
<th>Balance Outstanding</th>
<th>Balance Outstanding</th>
<th>New Loan</th>
<th>Principal Repayment for $l_month</th>
<th>Principal Repaid</th>
<th>Balance Outstanding</th>
<th>Interest Payment for $l_month </th>
<th>INTEREST ACCUMULATED DURING FINNANCIAL YEAR</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['app_file_id'] . "</td>";
echo"<td>". $row['app_fname']."</td>";
echo"<td>". $row['app_lname']."</td>";
echo"<td>". $row['commit_date'] ."</td>";
echo"<td>". $row['computer_interest'] ."</td>";
echo"<td>". $row['loan_life'] ."</td>";
echo"<td>". $row['avg(app_ln_amnt)'] ."</td>";
echo"<td>". $row['Balance Outstanding'] ."</td>";
echo"<td>". $row['New Loan'] ."</td>";
echo"<td>". $row['SUM(r.receipt_on_principal)'] ."</td>";
echo"<td>". $row['Principal Repaid'] ."</td>";
echo"<td>". $row['avg(app_amnt_owed)'] ."</td>";
echo"<td>". $row['SUM(r.receipt_on_interest)'] ."</td>";
echo"<td>". $row['Interest Accumilated'] ."</td>";
echo "</tr>";
}
echo "</table>";
?>
PHP 不会将事件发送到浏览器。你可以用Javascript做到这一点。
<input type="button" onclick="window.print()" value="Print Table" />
要隐藏您不想打印的所有其他内容,请将其添加到您的 html 中:
<style media="printer">
.noprint * {
display:none;
}
并将 css 类 noprint 分配给您不想打印的父元素。
未经测试,但这也可以工作:
body {
visibility: hidden;
}
.printthis {
visibility: visible;
}
并给你的表类打印这个,所以只打印表。
我是 php 的新手,但我知道做它们函数的唯一方法是通过 javascript。
下面是一些代码,它将显示一个打印按钮并将其发送到打印机,如果使用谷歌浏览器,您将首先出现打印预览屏幕,使用其他浏览器,它将仅打开小打印窗口。
<SCRIPT LANGUAGE="JavaScript">
if (window.print) {
document.write('<form><input type=button name=print value="Print Page"onClick="window.print()"></form>');
}
</script>
对于网站,您必须非常小心格式,http讨厌打印,因此最好在页面上使用最少的脚本,否则页面的一半将是间距。 特别是如果使用下拉菜单之类的东西,例如打印所有
- 和
- 语句转换为页面下方的项目符号。否则,我可能会建议将页面显示为pdf,这样您就可以更好地控制页面布局。
<?php
echo "<table border='1' id= results>
<tr>
<th>FILEID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Issue Date</th>
<th>Interest Rate</th>
<th>Terms</th>
<th>Balance Outstanding</th>
<th>Balance Outstanding</th>
<th>New Loan</th>
<th>Principal Repayment for $l_month</th>
<th>Principal Repaid</th>
<th>Balance Outstanding</th>
<th>Interest Payment for $l_month </th>
<th>INTEREST ACCUMULATED DURING FINNANCIAL YEAR</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['app_file_id'] . "</td>";
echo"<td>". $row['app_fname']."</td>";
echo"<td>". $row['app_lname']."</td>";
echo"<td>". $row['commit_date'] ."</td>";
echo"<td>". $row['computer_interest'] ."</td>";
echo"<td>". $row['loan_life'] ."</td>";
echo"<td>". $row['avg(app_ln_amnt)'] ."</td>";
echo"<td>". $row['Balance Outstanding'] ."</td>";
echo"<td>". $row['New Loan'] ."</td>";
echo"<td>". $row['SUM(r.receipt_on_principal)'] ."</td>";
echo"<td>". $row['Principal Repaid'] ."</td>";
echo"<td>". $row['avg(app_amnt_owed)'] ."</td>";
echo"<td>". $row['SUM(r.receipt_on_interest)'] ."</td>";
echo"<td>". $row['Interest Accumilated'] ."</td>";
echo "</tr>";
}
echo "</table>";
?>
<button onclick="javascript:window.print();"></button>
我会在最后添加这个JavaScript:
<script type="text/javascript">
function printpage()
{
window.print()
}
</script>
<?php
echo "<table border='1' id= results>
<tr>
<th>FILEID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Issue Date</th>
<th>Interest Rate</th>
<th>Terms</th>
<th>Balance Outstanding</th>
<th>Balance Outstanding</th>
<th>New Loan</th>
<th>Principal Repayment for $l_month</th>
<th>Principal Repaid</th>
<th>Balance Outstanding</th>
<th>Interest Payment for $l_month </th>
<th>INTEREST ACCUMULATED DURING FINNANCIAL YEAR</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['app_file_id'] . "</td>";
echo"<td>". $row['app_fname']."</td>";
echo"<td>". $row['app_lname']."</td>";
echo"<td>". $row['commit_date'] ."</td>";
echo"<td>". $row['computer_interest'] ."</td>";
echo"<td>". $row['loan_life'] ."</td>";
echo"<td>". $row['avg(app_ln_amnt)'] ."</td>";
echo"<td>". $row['Balance Outstanding'] ."</td>";
echo"<td>". $row['New Loan'] ."</td>";
echo"<td>". $row['SUM(r.receipt_on_principal)'] ."</td>";
echo"<td>". $row['Principal Repaid'] ."</td>";
echo"<td>". $row['avg(app_amnt_owed)'] ."</td>";
echo"<td>". $row['SUM(r.receipt_on_interest)'] ."</td>";
echo"<td>". $row['Interest Accumilated'] ."</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='"button'" value='"Print this page'" onclick='"printpage()'" />";
?>
如果您希望在用户打开此文档时打印机将打开,您可以使用 Body 中的 OnLoad 事件中的函数:
<body onload="printpage()">
祝你好运:)