通过打印机使用打印功能打印php表格

通过打印机使用打印功能打印php表格,php,mysql,printing,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>

我有下面的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

下面是一些显示打印按钮并将其发送到打印机的代码,如果使用google chrome,您将首先看到打印预览屏幕,使用其他浏览器,它将只打开小的打印窗口

<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()\" />";
    ?>
    
    
    函数printpage()
    {
    window.print()
    }
    
    如果您希望在用户打开此文档时打印机将打开,您可以使用正文中的onload事件中的功能:

    <body onload="printpage()">
    
    
    

    祝你好运:)

    PHP不会将事件发送到浏览器。您可以使用Javascript实现这一点

    要隐藏所有其他不想打印的内容,请将其添加到html中:

    <style media="printer">
          .noprint * {
              display:none;
          }
    

    并将printthis类指定给您的表,这样就只打印该表。

    您使用什么应用程序来打印它?你的意思是你只想打印代码,还是只想打印处理过的html?“只得到我文档的html部分”,你是什么意思?此外,表结构是无效的HTML。这是php代码的一部分。我正在尝试打印表中的结果,但我得到的只是页面的模板,没有结果,模板是HTML部分示例、导航栏、页眉图像页脚等,使用css隐藏其他元素-@media print{.noPrint{display:none;}php数据仍然没有打印:(你现在可以发布修改后的php脚本吗?猜脚本很难,你知道吗?:)@PierreGeier:你可以发布这个页面或工作html页面的完整脚本吗?因为我尝试了这个方法,但没有成功。很抱歉回答得太晚,请查看我的JSFIDLE。我注意到我的脚本示例中有一个输入错误,它必须是“打印机”。您只需要一个类或css选择器,它只有在打印页面时才可见。您可以通过更改css媒体类型来实现这一点。可以使用媒体查询,但它们没有得到很好的支持,仍然无法在表中打印php内容:(
    body {
        visibility: hidden;
    }
    .printthis {
        visibility: visible;
    }