我从数据库中获取值并显示在表中。我试图将结果打印为个体。 我使用下面的javascript
<script type="text/javascript">
function print_parent(element)
{
element.parentNode.className = 'print';
window.print();
element.parentNode.className = '';
return false;
}
</script>
我遇到的问题是当我尝试打印所有结果时效果很好。请告诉我如何在每个结果中打印每张表?
下面是我的PHP代码
$sql="select * from cisdb where pids LIKE '%$pids%'";
$result=mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result)==0) {
echo '<b><center>There was no records !</center></b>'."<br>";
}
while ($row=mysql_fetch_array($result)) {
$cat=str_replace('+', ' ', $row['category']);
print "<center>";
print "<table width='472' border='1' align='center' class='noprint'>";
print "<tr>";
print "<td width='150'><div align='center'><a href='#' onclick='return print_parent(this)'>Print</a>
</div></td>";
print "<td width='150'><div align='center'><a href='process.php?mode=ed&id={$row['id']}'>Edit</div></td>";
print "<td width='150'><div align='center'><a href='process.php?mode=del&id={$row['id']}' onclick='return confirm('Are you sure you want to delete?')'>Delete</a></div></td>";
print "</tr>";
print "</table><br>";
print "<div id='divToPrint'>";
print"<table width=700 style=height:900 border=1 cellpadding=1 cellspacing=1 bordercolor=#D6D6D6 class=sss title={$row['title']}>
<tr>
<td height=25 colspan=2 align=left valign=top><strong>Customer:{$row['name']}</strong></td>
<td width=183 align=left valign=top><strong>Sales ID:{$row['said']} </strong></td>
<td width=100 align=left valign=top><strong>Phone Cord. ID:{$row['pcid']}</strong></td>
<td align=left valign=top><strong>Type:{$row['classtype']}</strong></td>
</tr>
<tr>
<td height=25 colspan=2 valign=top><strong>Contact Name: </strong></td>
<td colspan=2 valign=top><strong>Email:</strong></td>
<td width=154 valign=top><strong>Phone:</strong></td>
</tr
<tr>
<td height=15 colspan=5 valign=top><strong>Remarks:</strong></td>
</tr>
<tr>
<td height=15 colspan=2 valign=top><strong>Date Added: </strong></td>
<td valign=top><strong>Date Edited : </strong></td>
<td colspan=2 valign=top><strong>Printed : </strong></td>
</tr>
</table></div><br>";
print "</center>";
}
答案 0 :(得分:0)
坚持使用此代码并将id = print添加到您想要打印的任何元素中!
使用这个jQuery:
$("#print")
.attr( "href", "javascript:void( 0 )" )
.click(
function(){
// Print the DIV.
$(".printable").print();
// Cancel click event.
return( false );
});
这个js文件:
// Create a jquery plugin that prints the given element.
jQuery.fn.print = function(){
// NOTE: We are trimming the jQuery collection down to the
// first element in the collection.
if (this.size() > 1){
this.eq( 0 ).print();
return;
} else if (!this.size()){
return;
}
// ASSERT: At this point, we know that the current jQuery
// collection (as defined by THIS), contains only one
// printable element.
// Create a random name for the print frame.
var strFrameName = ("printer-" + (new Date()).getTime());
// Create an iFrame with the new name.
var jFrame = $( "<iframe name='" + strFrameName + "'>" );
// Hide the frame (sort of) and attach to the body.
jFrame
.css( "width", "1px" )
.css( "height", "1px" )
.css( "position", "absolute" )
.css( "left", "-9999px" )
.appendTo( $( "body:first" ) )
;
// Get a FRAMES reference to the new frame.
var objFrame = window.frames[ strFrameName ];
// Get a reference to the DOM in the new frame.
var objDoc = objFrame.document;
// Grab all the style tags and copy to the new
// document so that we capture look and feel of
// the current document.
// Create a temp document DIV to hold the style tags.
// This is the only way I could find to get the style
// tags into IE.
var jStyleDiv = $( "<div>" ).append(
$( "style" ).clone()
);
// Write the HTML for the document. In this, we will
// write out the HTML of the current element.
objDoc.open();
objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
objDoc.write( "<html>" );
objDoc.write( "<body>" );
objDoc.write( "<head>" );
objDoc.write( "<title>" );
objDoc.write( document.title );
objDoc.write( "</title>" );
objDoc.write( jStyleDiv.html() );
objDoc.write( "</head>" );
objDoc.write( this.html() );
objDoc.write( "</body>" );
objDoc.write( "</html>" );
objDoc.close();
// Print the document.
objFrame.focus();
objFrame.print();
// Have the frame remove itself in about a minute so that
// we don't build up too many of these frames.
setTimeout(
function(){
jFrame.remove();
},
(60 * 1000)
);
}
答案 1 :(得分:0)
将表格包装在
中 <div class="printable">
你的桌子
</div>
并使用
按照上面的建议使用jquery$(".printable").print();