2

我有以下 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>";
?>
4

4 回答 4

4

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;
}

并为您的表格提供 printthis 类,因此仅打印表格。

于 2012-04-16T13:01:30.140 回答
1

我是 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 讨厌打印,因此页面上最好有最少的脚本,否则页面的一半将是间距。特别是如果在打印所有 <ul> 和 <li> 语句时使用下拉菜单之类的内容,则该语句将转换为页面下方的项目符号。否则,我可能会建议您将页面显示为 pdf,这样您就可以更好地控制页面布局。

于 2012-04-16T12:57:40.847 回答
0
<?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>
于 2012-04-16T12:59:04.577 回答
0

我会在最后添加这个 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()\" />";
?>

如果您希望在用户打开此文档时打印机将打开,您可以在正文的 onload 事件中使用该功能:

<body onload="printpage()">

祝你好运 : )

于 2012-04-16T12:59:55.523 回答