Datatable autorefresh正在改变表

时间:2017-11-20 19:38:32

标签: php mysql datatables refresh

我有这个 table.php



<?php 
 $connect = mysqli_connect("dbexample.com", "dboexample", "password", "dbexample"); 
 $query ="SELECT * FROM te_lb_load_board, te_lb_load_board_cstm WHERE te_lb_load_board.id = te_lb_load_board_cstm.id_c AND te_lb_load_board_cstm.load_status_c LIKE '%open%' ORDER BY shipping_date_c DESC, shipping_time_c ASC"; 
 $result = mysqli_query($connect, $query); 
 ?> 
 <!DOCTYPE html> 
 <html> 
      <head> 
           <title>Load Board</title> 
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 
           <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> 
           <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
           <script src="https://cdn.datatables.net/plug-ins/1.10.15/api/fnReloadAjax.js"></script>      
           <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
           <script> 
 $(document).ready(function(){ 
      $('#employee_data').DataTable(); 
 }); 
 setInterval(function () {
      table.fnReloadAjax();
}, 3000);
 </script> 
      </head> 
      <body> 
           <br /><br /> 
           <div class="container"> 
                <h3 align="center">Load Board</h3> 
                <br /> 
                <div class="table-responsive"> 
                     <table id="employee_data" class="table table-striped table-bordered"> 
                          <thead> 
                               <tr>   
                                    <td>Date</td> 
                                    <td>Time</td>
                                    <td>Zip</td> 
                                    <td>City</td> 
                                    <td>Country</td> 
                                    <td>Date</td>
                                    <td>Time</td>
                                    <td>Zip</td>
                                    <td>City</td>
                                    <td>Country</td>
                                    <td>Description</td>
                               </tr> 
                          </thead> 
                          <?php 
                          while($row = mysqli_fetch_array($result)) 
                          { 
                               echo ' 
                               <tr>   
                                    <td>'.$row['shipping_date_c'].'</td> 
                                    <td>'.$row['shipping_time_c'].'</td>
                                    <td>'.$row["billing_address_postalcode"].'</td> 
                                    <td>'.$row["billing_address_city"].'</td> 
                                    <td>'.$row["billing_address_country"].'</td>
                                    <td>'.$row['arrival_date_c'].'</td>
                                    <td>'.$row['arrival_time_c'].'</td>
                                    <td>'.$row["shipping_address_postalcode"].'</td>
                                    <td>'.$row["shipping_address_city"].'</td>
                                    <td>'.$row["shipping_address_country"].'</td>
                                    <td>'.$row["description"].'</td> 
                               </tr> 
                               '; 
                          } 
                          ?> 
                     </table> 
                </div> 
           </div> 
      </body> 
  
 </html>  
&#13;
&#13;
&#13;

我使用以下代码创建一个index.php文件:

&#13;
&#13;
<title>Glastopf Observation Center</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
 
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#results').load('table.php'); // table.php is where I have my table
}, 3000); // the "3000" here refers to the time to refresh the div. it is in milliseconds.
});
// ]]></script>
 
<div id="results">Loading data ...</div>
&#13;
&#13;
&#13;

它正在工作,我在桌面上获得了我的数据的自动刷新但是我每3秒就有这个css问题:

This is how it looks as usual

This is how it looks after 3 seconds

我把css链接放到我之前提到的index.php文件中,但是我仍然有相同的css问题:

&#13;
&#13;
<title>Glastopf Observation Center</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 
           <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> 
           <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
           <script src="https://cdn.datatables.net/plug-ins/1.10.15/api/fnReloadAjax.js"></script>      
           <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
 
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#results').load('table.php');
}, 3000); // the "3000" here refers to the time to refresh the div. it is in milliseconds.
});
// ]]></script>
 
<div id="results">Loading data ...</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我开始工作了......

在你的table.php中(你必须使用PDO而不是MySQLi):

SqlCommand cmd = new SqlCommand("SELECT * FROM Personel where Kode_Personel LIKE '%P%' ", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
sda.Fill(dt);
con.Close();

int j = 0;
 foreach (DataRow row in dt.Rows)
 {

     j++;
     cekjabatan = dt.Rows[0].ItemArray[2].ToString(); //this value not change to next row
     cekkode = dt.Rows[0].ItemArray[0].ToString(); //this value not change to next row

     if (cekkode.Contains("A") && cekkode.Contains("P") || cekkode.Contains("B") && cekkode.Contains("P") || cekkode.Contains("C") && cekkode.Contains("P"))
     {
        ceksubstring = cekkode.Substring(0, 4);
     }
     else if (cekkode.Contains("P") && !cekkode.Contains("B"))
     {
        ceksubstring = cekkode.Substring(0, 3);
     }
 }

在index.php中:

<?php 
$pdo = new PDO("mysql:dbname=dbexample;host=dbexample.com;port=3306", "dboexample", "password");
$query = $pdo->prepare("SELECT * FROM te_lb_load_board, te_lb_load_board_cstm WHERE te_lb_load_board.id = te_lb_load_board_cstm.id_c AND te_lb_load_board_cstm.load_status_c LIKE '%open%' ORDER BY shipping_date_c DESC, shipping_time_c ASC");
$query->execute();
echo json_encode($query->fetchAll());
?>