php - Auto Refresh a Html table every x seconds -


i attempting refresh table have variables in there updated , want re-update variable every few seconds. have done code on part giving table id , creating div it. code explain actually. in advance ! edited: added var table , var refresher. code still wont reload table !any ideas??

test.php

       <html> <head>     <script type='text/javascript'>  var table = $('#tableid');  // refresh every 5 seconds var refresher = setinterval(function() {   table.load("1103242b/processing/js.php"); }, 5000);  settimeout(function() {   cleartimeout(refresher); }, 1800000); </script> </head> <body> <?php include_once'js.php'; ?>  </body> </html> 

js.php

<?php require_once 'connect.php'; include 'start.php'; include 'functions.php'; header("cache-control: no-cache,no-store");  $query = "select * opentrades"; //you don't need ; in sql $result = mysql_query($query); echo "<table border = '1px' id='tableid'>"; // start table tag in html echo "<tr><td>" . "order number" . "</td><td>" . "selection" . "</td><td>" . "date" . "</td><td>" . "type" . "</td><td>" . "size" . "</td><td>" . "bid price" . "</td><td>" . "offer price" . "</td><td>" ."stop loss" . "</td><td>" . "take profit" . "</td><td>" ."profit/loss(usd)"."</td><td>" ."close"."</td></tr>" ;  //$row['index'] index here field name  while($row = mysql_fetch_assoc($result)){   //creates loop loop through results if ($row['selection']=='eur/usd')// retrieve bid , offer each row       {       $bidpricepl=$bid;        $offerpricepl=$bid1;        }   elseif ($row['selection']=='usd/jpy')       {       $bidpricepl=$bid2;        $offerpricepl=$bid3;        }   elseif ($row['selection']=='usd/cad')       {       $bidpricepl=$bid4;        $offerpricepl=$bid5;        }   elseif ($row['selection']=='eur/jpy')       {       $bidpricepl=$bid6;        $offerpricepl=$bid7;        }   elseif ($row['selection']=='eur/chf')       {       $bidpricepl=$bid8;        $offerpricepl=$bid9;        }   elseif ($row['selection']=='gbp/usd')       {       $bidpricepl=$bid10;        $offerpricepl=$bid11;        }   elseif ($row['selection']=='aud/usd')       {       $bidpricepl=$bid12;        $offerpricepl=$bid13;        }   elseif ($row['selection']=='usd/chf')       {       $bidpricepl=$bid14;        $offerpricepl=$bid15;        }   if ($row['type']=="buy")     {        $minipipskiller='10';       $offeropen=$row['offerprice'];       $pips=$offerpricepl-$offeropen;       $closedb=$offeropen;       $pips1=round($pips, 6);       $pips2 = str_replace('.', '', $pips1);         if ($pips2<0)       {         $pips2 = str_replace('-', '', $pips2);         $pips2 = ltrim($pips2, '0');         $pips2 = -1 * abs($pips2);       }       else {         $pips2 = ltrim($pips2, '0');       }       $pips3=$pips2/$minipipskiller;     }// pip counting   elseif ($row['type']=="sell")//for pip counting     {        $minipipskiller='10';       $bidopen=$row['bidprice'];       $pips=$bidopen-$bidpricepl;       $closedb=$bidopen;       $pips1=round($pips, 6);       $pips2 = str_replace('.', '', $pips1);       if ($pips2<0)       {         $pips2 = str_replace('-', '', $pips2);         $pips2 = ltrim($pips2, '0');         $pips2 = -1 * abs($pips2);       }       else {         $pips2 = ltrim($pips2, '0');       }       $pips3=$pips2/$minipipskiller;     }          $ticksize= "0.0001";// profit , loss       $lot1 = "100000";       $sizecalc=$row['size'] * $lot1;          if ($row['type']=="buy")       {          $profitandloss=$sizecalc*$ticksize*$pips3; //per trade       }       if ($row['type']=="sell")       {         $profitandloss=$sizecalc*$ticksize*$pips3; //per trade       }        $zero= '0';        if($profitandloss<$zero) {             $profittext = "<div style=\"color: red;\">$profitandloss</div>";         } elseif ($profitandloss>$zero) {             $profittext = "<div style=\"color: green;\">$profitandloss</div>";         }   $sum+= $profitandloss;       echo "<tr><td>" . $row['trade_id'] .                  "</td><td>" . $row['selection'] .          "</td><td>" . $row['date'] .         "</td><td>" . $row['type'] .         "</td><td>" . $row['size'] .         "</td><td>" . $row['bidprice'] .         "</td><td>" . $row['offerprice'] .         "</td><td>" . $row['stoploss'] .         "</td><td>" . $row['takeprofit'] .         "</td><td>" . $profittext .          "</td><td><a href ='delete.php?id=".         $row['trade_id']."'>x</a>        </td></tr>";   $profitandloss=0;  if($sum<$zero) {     $sumtext = "<div style=\"color: red;\">$sum</div>"; } elseif ($sum>$zero) {     $sumtext = "<div style=\"color: green;\">$sum</div>"; } }  echo "</table><br>";    ?> 

i think setinterval jquery.load 1 you're looking for

var table = $("#tableid");  // refresh every 5 seconds var refresher = setinterval(function() {   table.load("/path/to/js.php"); }, 5000); 

or shorten with

var refresher = setinterval(table.load.bind(table, "/path/to/data"), 5000); 

if you'd ever stop refreshing data, (e.g.,) user leaves page open long time

// stop refreshing after 30 minutes settimeout(function() {   cleartimeout(refresher); }, 1800000); 

if data load takes while, might want refresh x seconds after data loaded. using settimeout

var table = $("#tableid");  var refresh = function() {   table.load("/path/to/js.php", function() {     settimeout(refresh, 5000);   }); };  refresh(); 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -