Print php table with Print function via Printer

51,797

Solution 1

PHP doesn't send Events to a Browser. You can do that with Javascript.

<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:

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

And assign the css class noprint to the parent element you don't want to get printed.

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.

Solution 2

I am new to php, but the only way I know of doing them functions is through 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.

Share:
51,797
dames
Author by

dames

Updated on September 30, 2020

Comments

  • dames
    dames almost 4 years

    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
    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>";
    ?>