php 通过打印机使用打印功能打印php表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10174412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print php table with Print function via Printer
提问by dames
I have the following php table, how do i add print functionality just to the php table? And a button that when clicked, the following table is printed via printer, I tried 'CTRL+P' and I only got the html secton of the page example the header, footer, navigation bar, and not the results php results
我有以下 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>";
?>
回答by Pierre Geier
PHP doesn't send Events to a Browser. You can do that with Javascript.
PHP 不会将事件发送到浏览器。你可以用 Javascript 做到这一点。
<input type="button" onclick="window.print()" value="Print Table" />
<input type="button" onclick="window.print()" value="Print Table" />
To hide all the other Stuff you don't want to print add this to your html:
要隐藏您不想打印的所有其他东西,请将其添加到您的 html:
<style media="printer">
.noprint * {
display:none;
}
And assign the css class noprint to the parent element you don't want to get printed.
并将 css 类 noprint 分配给您不想打印的父元素。
Not tested but this could work too:
未经测试,但这也可以:
body {
visibility: hidden;
}
.printthis {
visibility: visible;
}
And give your table the class printthis and so only the table gets printed.
并为您的表格提供类 printthis,因此只有表格会被打印。
回答by Robin
I am new to php, but the only way I know of doing them functions is through javascript.
我是 php 的新手,但我知道执行它们的唯一方法是通过 javascript。
Below is some code which will display a print button and send it to a printer, if using google chrome you will get a print preview screen come up first, with other browsers it will solely open up the small printing window.
下面是一些将显示打印按钮并将其发送到打印机的代码,如果使用谷歌浏览器,您将首先出现打印预览屏幕,而其他浏览器将仅打开小打印窗口。
<SCRIPT LANGUAGE="JavaScript">
if (window.print) {
document.write('<form><input type=button name=print value="Print Page"onClick="window.print()"></form>');
}
</script>
With websites you have to be very careful with the formatting, http hates to print, so it is best to have minimal scripts on the page, otherwise half of the page will be spacing. especially if using things like drop down menus as in printing all of the < ul > and < li > statements convert to bullet points down the page. Otherwise I would probably recommend to look at displaying the page as pdf, where you would have much more control over the page layout.
对于网站,您必须非常注意格式,http 讨厌打印,因此最好在页面上使用最少的脚本,否则页面的一半将被留空。特别是如果使用诸如下拉菜单之类的东西来打印所有 <ul> 和 <li> 语句将转换为页面下方的项目符号。否则,我可能会建议将页面显示为 pdf,这样您就可以更好地控制页面布局。
回答by Mohit Bumb
<?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>
回答by Maor Tal
I'd add this JavaScript at the end:
我会在最后添加这个 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()\" />";
?>
if you wish while user open this document the printer will turn on you can use the function in onload event in body:
如果您希望在用户打开此文档时打印机将打开,您可以在正文中的 onload 事件中使用该功能:
<body onload="printpage()">
Good luck : )
祝你好运 : )

