【问题标题】:Print php table with Print function via Printer通过打印机打印带有打印功能的 php 表
【发布时间】:2012-04-16 12:50:33
【问题描述】:

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

【问题讨论】:

  • 您用什么应用程序来打印这个?你的意思是你只想打印代码,还是处理后的html?
  • "只得到了我文档的html部分",什么意思?此外,表结构是无效的 HTML。
  • 它是一段 php 代码我正在尝试从表格中打印结果,但我得到的只是页面模板,没有结果,模板是 html 部分示例,导航栏,页眉图片页脚等
  • 使用 css 隐藏其他元素 - @media print { .noPrint { display:none; } }

标签: php mysql printing


【解决方案1】:

PHP 不会向浏览器发送事件。你可以用 Javascript 做到这一点。

&lt;input type="button" onclick="window.print()" value="Print Table" /&gt;

要隐藏您不想打印的所有其他东西,请将其添加到您的 html:

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

并将 css 类 noprint 分配给您不想打印的父元素。

未经测试,但这也可以工作:

body {
    visibility: hidden;
}
.printthis {
    visibility: visible;
}

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

【讨论】:

  • php 数据仍未打印 :(
  • 您现在可以发布您修改后的 PHP 脚本吗?猜剧本很难,你知道吗? :)
  • @PierreGeier:你能发布完整的脚本或工作的 html 页面吗?因为我试过了,但没有成功。
  • 抱歉回答晚了,看看我的jsfiddle。 jsfiddle.net/hBCgA 我注意到我的脚本示例中有错字,它必须是“打印机”。您只需要一个仅在打印页面时可见的类或 CSS 选择器。您可以通过更改 css 媒体类型来做到这一点。可以使用媒体查询,但它们的支持不是很好css-tricks.com/css-media-queries
【解决方案2】:

我是 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,这样您可以更好地控制页面布局。

【讨论】:

    【解决方案3】:
    <?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>
    

    【讨论】:

      【解决方案4】:

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

      祝你好运:)

      【讨论】:

      • 它仍然没有打印表格中的php内容:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      相关资源
      最近更新 更多