通过打印机打印带打印功能的php表

时间:2012-04-16 12:50:34

标签: php mysql printing

我有以下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 个答案:

答案 0 :(得分: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,因此只打印表格。

答案 1 :(得分: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讨厌打印,因此最好在页面上使用最少的脚本,否则页面的一半将是间距。特别是如果使用下拉菜单之类的东西,如打印所有&lt; ul&gt;和&lt; li>语句转换为页面下方的项目符号。否则我可能会建议将页面显示为pdf,您可以更好地控制页面布局。

答案 2 :(得分: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>

答案 3 :(得分: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()">
祝你好运:)