Php 打印单个表格?

Php 打印单个表格?,php,javascript,html,Php,Javascript,Html,我从数据库中获取值并显示在表中。我试着打印结果作为个人。 我正在使用下面的javascript <script type="text/javascript"> function print_parent(element) { element.parentNode.className = 'print'; window.print(); element.parentNode.className = ''; return false; } </script>

我从数据库中获取值并显示在表中。我试着打印结果作为个人。 我正在使用下面的javascript

<script type="text/javascript">
function print_parent(element)
{
  element.parentNode.className = 'print';
  window.print();
  element.parentNode.className = '';
  return false;
}
</script>

函数打印\父级(元素)
{
element.parentNode.className='print';
window.print();
element.parentNode.className='';
返回false;
}
我遇到的问题是,当我尝试打印所有结果时,效果很好。有人能告诉我如何打印每个结果中的每个表吗

下面是我的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>";

        }
$sql=“从cisdb中选择*,其中PID类似“%$pids%””;
$result=mysql\u query($sql)或die(mysql\u error());
if(mysql_num_rows($result)==0){
echo“没有记录!”。“
”; } while($row=mysql\u fetch\u数组($result)){ $cat=str_replace(“+”,“$row['category]”); 打印“”; 打印“”; 打印“”; “打印” "; 打印“”; 打印“”; 打印“
”; 打印“”; “打印” 客户:{$row['name']} 销售ID:{$row['said']} 电话线。ID:{$row['pcid']} 类型:{$row['classtype']} 联系人姓名: 电子邮件: 电话:
将此代码插入并将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)
        );
}
//创建打印给定元素的jquery插件。
jQuery.fn.print=函数(){
//注意:我们正在将jQuery集合缩减到
//集合中的第一个元素。
if(this.size()>1){
这个.eq(0).print();
返回;
}否则如果(!this.size()){
返回;
}
//断言:此时,我们知道当前jQuery
//集合(如本文所定义)仅包含一个
//可打印元素。
//为打印框创建随机名称。
var strFrameName=(“打印机-”+(新日期()).getTime());
//使用新名称创建iFrame。
var jFrame=$(“”);
//隐藏框架(某种程度上)并附加到主体。
jFrame
.css(“宽度”、“1px”)
.css(“高度”、“1px”)
.css(“位置”、“绝对”)
.css(“左”,“-9999px”)
.appendTo($(“正文:第一个”))
;
//获取新帧的帧引用。
var objFrame=window.frames[strFrameName];
//获取对新框架中DOM的引用。
var objDoc=objFrame.document;
//抓取所有样式标记并复制到新的
//文档,以便我们捕获
//当前文档。
//创建临时文档DIV以保存样式标记。
//这是我能找到的唯一得到这种风格的方法
//将标签插入IE。
var jStyleDiv=$(“”)。追加(
$(“样式”).clone()
);
//为文档编写HTML。在这里,我们将
//写出当前元素的HTML。
objDoc.open();
objDoc.write(“”);
objDoc.write(“”);
objDoc.write(“”);
objDoc.write(“”);
objDoc.write(“”);
objDoc.write(document.title);
objDoc.write(“”);
write(jStyleDiv.html());
objDoc.write(“”);
write(this.html());
objDoc.write(“”);
objDoc.write(“”);
objDoc.close();
//打印文档。
objFrame.focus();
objFrame.print();
//在大约一分钟内将框架自行拆除,以便
//我们不会建立太多这样的框架。
设置超时(
函数(){
jFrame.remove();
},
(60 * 1000)
);
}
将您的表格用

你的桌子在这儿吗

并使用上面建议的jquery

$(".printable").print();

我相信这个问题与PHP无关。制作一个干净的HTML页面,并尝试使用it@Col.弹片,用纯html打印单个表不是问题。我不认为用纯html编码循环是可能的:D。我的问题是如何打印循环中的每个表?html中没有循环。尝试lise that我知道,你说这个问题与php无关,那么如何才能使用纯html从DB和循环中获取输出。我想知道的是如何在循环中打印每个单独的结果DB和循环的输出结果是纯html。html中没有循环。你的javascript在浏览器中运行,其中是纯HTML,没有循环。