0

So i have managed to finally populate datatable, but since there is some updates going in in mySQL all the time, i am looking for way to refresh/reload newest data from mySQL.

So far i have this in data.php

    <?php
    include "connect.php";
    $sqlAdmin = mysqli_query($connection, "SELECT FROM_UNIXTIME(lastChange) as new_date,pair, percentage, delta_t, current_price, last_price FROM testdatabase.table ORDER BY id DESC LIMIT 0,100;");    
    $locations = array();
    while($res = mysqli_fetch_array($sqlAdmin)) {
        $locations[] = array($res['new_date'], $res['pair'], $res['percentage'], $res['delta_t'], $res['current_price'],$res['last_price']);
  }
?>

and datatable is in index.php (shorter version with only important stuff)

<!-- TABLE -->
<table id="example" class="display compact myTableClass">
    <thead>
        <tr>
        <th scope="col">Date</th>
        <th scope="col">Pairing</th>
        <th scope="col">Percentage</th>
        <th scope="col">Delta time</th>
        <th scope="col">Current price</th>
        <th scope="col">Past Price</th>
        </tr>
    </thead>
    <tbody id="myTable" class="dark-theme-table">
    </tbody>
</table>
           
<!-- LOAD DATA FROM MYSQL -->
<?php   include "data.php";  ?>
          
<script>
    // load data from php to variable
    var mydata  = <?php echo json_encode($locations); ?>;

    console.log(mydata);
        
    // this should reload new data every 5 seconds 
    var refreshId = setInterval(function()
    {
        <?php  include "data.php"; ?>
        mydata  = <?php echo json_encode($locations); ?>;
        console.log('Refresh ... ',mydata)
    }, 5000);

    var table = $('#example').DataTable({
        data: mydata
        // a lot of additional options, not important to show
        });
        
    $(document).ready(function() {
      table.draw();
    });
      
</script>

My main problem is if I do it this way, refreshed data are not newest... I am checking in console if new data were loaded but so far no success

anyone have an idea how to solve this ? should i approach this problem differently ? should i put entire datatable into new .php and refresh that ?

Regards

0 Answers0