-1

The code below is my way on how to print my data in Table.

<style media="screen">
  .noPrint{ display: block; }
  .yesPrint{ display: block !important; }
</style>

<style media="print">
  .noPrint{ display: none; }
  .yesPrint{ display: block !important; }
</style>

Here is my code of printing the data from the table

  <div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;">
    <div class="CSSTableGenerator" >
    <div class= "yesPrint">
    <?php
    $con = mysql_connect("localhost","root","");
    mysql_select_db("dbreport",$con);
    $sql = "select * from tblreport";
    $mydata = mysql_query($sql,$con);
    echo "<table border=1 id='tbody'>
    <tr>
    <th>Crime Name</th>
    <th>Time</th>
    <th>Date</th>
    <th>Address</th>
    <th>Detail</th>
    </tr>";

    while ($record = mysql_fetch_array($mydata)){

    echo "<tr>";
    echo "<td>" . $record['Crime Name'] . "</td>";
    echo "<td>" . $record['Time'] . "</td>";
    echo "<td>" . $record['Date'] . "</td>";
    echo "<td>" . $record['Address'] . "</td>";
    echo "<td>" . $record['Detail'] . "</td>";
    echo "</tr>";



    }
    echo "</table>";
    mysql_Close($con);
    ?>
    </div>
    </div>
    </div>

To initiate the command

<input TYPE="button" value = "Print Report" onClick="window.print()">

Further Explanation: My code here is to print the data from my table and the table has been populated from my database. Data Transfer to Table then Print but what happens is the whole website has been print. It seems that my code Capture the site as Image and this what it print.

What I want to do is print what only on the table, how can achieve it? TY

  • I don't get what you are asking, you are looping through the RESULTS of a query, which will display what's in the table?????? – Jordy Dec 10 '14 at 17:40
  • possible duplicate of [Print specific part of webpage](http://stackoverflow.com/questions/12997123/print-specific-part-of-webpage) –  Dec 10 '14 at 17:49
  • The easiest way is to use `@media print` in your CSS to define how you want the printed document to look (and hide parts your *don't* want) – Phil Tune Dec 10 '14 at 17:49
  • I just want to print the data on my table. and my table is filled with data from my database – Abduh Mamba Umbrakato Dec 10 '14 at 17:49
  • @Caddaile You post a possible duplicate along with an answer; that's not very fair. – Funk Forty Niner Dec 10 '14 at 17:50
  • My apologies, I realized I can flag it after I posted, and forgot to delete - done by now, thanks! –  Dec 10 '14 at 17:53

2 Answers2

0

try this,

you r using mysql_Close($con); but in real mysql_close($con);

<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;">
<div class="CSSTableGenerator" >
<div class= "yesPrint">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("dbreport",$con);
$sql = "select * from tblreport";
$mydata = mysql_query($sql,$con);
echo "<table border=1 id='tbody'>
<tr>
<th>Crime Name</th>
<th>Time</th>
<th>Date</th>
<th>Address</th>
<th>Detail</th>
</tr>";

while ($record = mysql_fetch_array($mydata)){

echo "<tr>";
echo "<td>" . $record['Crime Name'] . "</td>";
echo "<td>" . $record['Time'] . "</td>";
echo "<td>" . $record['Date'] . "</td>";
echo "<td>" . $record['Address'] . "</td>";
echo "<td>" . $record['Detail'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
</div>
</div>
jay.jivani
  • 1,560
  • 1
  • 16
  • 33
0

With the below javascript function, you can print any element by just placing the element in the parentheses.

<input type="button" value="Print Div" onclick="PrintElem('.yesPrint')" />

Here is the entire code:

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script> 
<script type="text/javascript">

    function PrintElem(elem)
    {
        Popup($(elem).html());
    }

    function Popup(data) 
    {
        var mywindow = window.open('', 'my div', 'height=400,width=600');
        mywindow.document.write('<html><head><title>my div</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');

        mywindow.print();
        mywindow.close();

        return true;
    }

</script>

<div style = "border:1px solid; height:360px; width:1180px; left: 180px; position: absolute; top: 95px; overflow-x: auto;" class="noPrint">
    <div class="CSSTableGenerator" >
        <div class= "yesPrint">
            <?php
            $con = mysql_connect("localhost","root","");
            mysql_select_db("dbreport",$con);
            $sql = "select * from tblreport";
            $mydata = mysql_query($sql,$con);
            ?>
            <table border=1 id='tbody'>
                <tr>
                    <th>Crime Name</th>
                    <th>Time</th>
                    <th>Date</th>
                    <th>Address</th>
                    <th>Detail</th>
                </tr>
                <?php
                while ($record = mysql_fetch_array($mydata)){

                    echo "<tr>";
                    echo "<td>" . $record['Crime Name'] . "</td>";
                    echo "<td>" . $record['Time'] . "</td>";
                    echo "<td>" . $record['Date'] . "</td>";
                    echo "<td>" . $record['Address'] . "</td>";
                    echo "<td>" . $record['Detail'] . "</td>";
                    echo "</tr>";
                }
                ?>
            </table>
            <?php mysql_close($con); ?>
        </div>
    </div>
</div>

<input type="button" value="Print Div" onclick="PrintElem('.yesPrint')" />
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115