2010-03-30 15 views
0

Je reçois des valeurs d'une base de données et affiche dans une table. Im essayant d'imprimer les résultats en tant qu'individu. Im en utilisant le javascript ci-dessousimprimer des tableaux individuels?

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

Le problème que j'ai est quand je tente d'imprimer tous les résultats, il fonctionne great.can quelqu'un s'il vous plaît me dire comment puis-je imprimer chaque table dans chaque résultat s'il vous plaît?

ci-dessous est mon code 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

Je crois que cette question n'a aucun rapport avec PHP. Faire une page HTML propre et aller expérimenter avec elle –

+0

@Col. Shrapnel, impression de tableaux individuels avec html ordinaire n'est pas un problème. Je ne pense pas qu'il soit possible de coder des boucles avec du code HTML: D. Le problème que j'ai est comment imprimer chaque table individuelle dans la boucle? – LiveEn

+0

Il n'y a pas de boucles dans le code HTML. Essayez de réaliser que –

Répondre

0

bâton ce code et mettre id = impression à tout élément que vous voulez imprimer!

Utilisez cette jQuery:

$("#print") 
        .attr("href", "javascript:void(0)") 
        .click(
         function(){ 
          // Print the DIV. 
          $(".printable").print(); 

          // Cancel click event. 
          return(false); 
         }); 

Et ce fichier 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) 
     ); 
} 
0

envelopper votre table dans un

<div class="printable">

votre table ici

</div>

et l'utilisation jquery comme suggéré ci-dessus en utilisant

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