Php 如何在没有数据表的情况下打印页面

Php 如何在没有数据表的情况下打印页面,php,jquery,html,datatable,Php,Jquery,Html,Datatable,我试图出于记录目的打印sql中的数据,但我使用的是datatable,因此当我尝试单击“打印”时,记录不会显示所有内容。它仅显示数据表第1页中的当前数据。我该怎么做?另外,当我尝试打印它时,屏幕上还显示了php的include函数。允许使用Javascript解决方案。这是我的密码 <?php include ('sidebar.php'); ?> <main id="playground"> <?php include ('head

我试图出于记录目的打印sql中的数据,但我使用的是datatable,因此当我尝试单击“打印”时,记录不会显示所有内容。它仅显示数据表第1页中的当前数据。我该怎么做?另外,当我尝试打印它时,屏幕上还显示了php的include函数。允许使用Javascript解决方案。这是我的密码

        <?php include ('sidebar.php'); ?>
    <main id="playground">
    <?php include ('header.html'); ?>

    <div class="container-fluid">
      <div class="row">
        <div class="col-md-12">


          <section class="panel panel-info">
            <header class="panel-heading">
              <h4 class="panel-title">List of employees</h4>
            </header>
            <div class="panel-body">

          <?php
            include('configuration.php');
            $sql = "SELECT  firstname, lastname, status, idnumber FROM employees ORDER BY lastname ASC";
            $result = $conn->query($sql);
          ?> 
              <table class="table table-striped datatable" id="datatables" >
                <thead>
                  <tr>
                    <th>Last Name</th>
                    <th>First Name</th>
                    <th>ID Number</th>
                  </tr>
                </thead>



                   <?php
                      if ($result->num_rows > 0) { // output data of each row?>
                       <tbody>
                        <button onclick="myFunction()">Print this page</button>
                           <?php  while($row = $result->fetch_assoc()) {
                              if($row['status']=='p'){
                      ?>

                              <?php {                         //this form will display the set of pending applications

                                      echo'<tr>';
                                      echo '<td>' . $row['lastname'] . '</td>';
                                      echo '<td>' . $row['firstname'] . '</td>';
                                      echo '<td>' . $row['idnumber'] . '</td>';
                                  echo'</tr>';
                                  }
                              ?>

                      <?php    } //if statement
                               } //while statement
                      ?>
                        </tbody>  
                      </table>


                      <?php
                      }else {
                          echo "0 results";
                      }
            ?>



            </div>
          </section>
          <!-- end of STRIPED ROWS TABLE -->

        </div> <!-- / col-md-12 -->
      </div> <!-- / row -->
    </div> <!-- / container-fluid -->


      </main> <!-- /playground -->




        <?php include ('notifications.html'); ?>



        <div class="scroll-top">
          <i class="ti-angle-up"></i>
        </div>
      </div> <!-- /animsition -->


    <script>
    function myFunction() {
        window.print();
    }
    </script>

      </body>
    </html>

雇员名单
姓
名字
身份证号码
打印此页
函数myFunction(){
window.print();
}
请使用

$('#datatables').DataTable( {
    buttons: [
        'print'
    ]
} );
请检查并使用以下两种功能:

function CreateTableFromObject(tblObj) {
    objHeader = JSON.parse(JSON.stringify(tblObj.buttons.exportData()))["header"];
    objRows = JSON.parse(JSON.stringify(tblObj.buttons.exportData()))["body"];

    //Check If Action Exists in Table and remove it
    var index = objHeader.indexOf('Action');
    if (index > -1) {
        objHeader.splice(index, 1);
    }

    tblToPrint = "<table style='border: 1px solid black; border-collapse: collapse;'><thead><tr>";
    $.each(objHeader, function (key, value) {
        tblToPrint += "<th style='border: 1px solid black;'>" + value + "</th>";
    });
    tblToPrint += "</tr></thead><tbody>";
    $.each(objRows, function (key, value) {
        tblToPrint += "<tr>";
        //If action exists then decrease target by 1
        if (index > -1) {
            target = value.length - 1;
        }else {
            target = value.length;
        }
        for (var i = 0; i < target; i++) {
            tblToPrint += "<td style='border: 1px solid black;'>" + value[i] + "</td>";
        }
        tblToPrint += "</tr>";
    });
    tblToPrint += "</tbody></table>";
    return tblToPrint;
}


function PrintWindowAddParts() {
        var tblObj = $("#YourTable").DataTable();
        var tblViewRMA = CreateTableFromObject(tblObj);
        var printContents = "<div class='dataTables_wrapper form-inline dt-bootstrap'>" + tblViewRMA + "</div>";
        var size = 'height=' + $(window).height() + 'px,width=' + $(window).width() + 'px';
        var mywindow = window.open('', 'PRINT', size);
        mywindow.document.write('<html><head><title>' + "My Title" + '</title>');
        mywindow.document.write('</head><body >');
        mywindow.document.write('<h4>' + "My Title" + '</h4>');
        mywindow.document.write(printContents);
        mywindow.document.write('</body></html>');
        mywindow.document.close();
        mywindow.focus();
        mywindow.print();
        mywindow.close();
        return true;
    }
函数CreateTableFromObject(tblObj){
objHeader=JSON.parse(JSON.stringify(tblObj.buttons.exportData())[“header”];
objRows=JSON.parse(JSON.stringify(tblObj.buttons.exportData())[“body”];
//检查表中是否存在操作并将其删除
var指数=objHeader.indexOf(“行动”);
如果(索引>-1){
objHeader.拼接(索引1);
}
tblToPrint=“”;
$.each(对象头、函数(键、值){
tblToPrint+=“”+值+“”;
});
tblToPrint+=“”;
$.each(对象行、函数(键、值){
tblToPrint+=“”;
//如果存在操作,则将目标值减少1
如果(索引>-1){
目标=值。长度-1;
}否则{
目标=value.length;
}
对于(变量i=0;i

您的打印功能已准备就绪。

可能重复使用
window.Print()你好!我使用了myFunction(),它调用window.print(),如果您看到我的代码:)@parthtrived如何解决问题?您可以使用datatable打印按钮。请查收