admin管理员组文章数量:1332649
I am attempting to refresh a table i have as the variables in there are constantly updated and i want to re-update those variable every few seconds. I have already done up code on my part by giving the table an id and creating a div for it. The code will explain what i actually. Thanks in Advance ! EDITED: Added Var Table and Var Refresher. however the code still wont reload my 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 * FROM opentrades"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table border = '1px' id='tableID'>"; // start a table tag in the 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'] the index here is a field name
while($row = mysql_fetch_assoc($result)){ //Creates a loop to loop through results
if ($row['selection']=='eur/usd')// TO RETRIEVE BID AND OFFER FOR 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";// FOR PROFIT AND 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 am attempting to refresh a table i have as the variables in there are constantly updated and i want to re-update those variable every few seconds. I have already done up code on my part by giving the table an id and creating a div for it. The code will explain what i actually. Thanks in Advance ! EDITED: Added Var Table and Var Refresher. however the code still wont reload my 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 * FROM opentrades"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table border = '1px' id='tableID'>"; // start a table tag in the 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'] the index here is a field name
while($row = mysql_fetch_assoc($result)){ //Creates a loop to loop through results
if ($row['selection']=='eur/usd')// TO RETRIEVE BID AND OFFER FOR 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";// FOR PROFIT AND 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>";
?>
Share
Improve this question
edited Aug 19, 2013 at 18:46
nigel
asked Aug 19, 2013 at 18:05
nigelnigel
1013 gold badges4 silver badges10 bronze badges
8
-
Replace
setInterval
withsetTimeout
. Use$("table.your-table").load()
and simply echo out the contents(!) of the table on thejs.php
(so no<table>
stuff) – AmazingDreams Commented Aug 19, 2013 at 18:06 - Where exactly is your error? What does not work as expected? – akluth Commented Aug 19, 2013 at 18:09
- The dynamic variables in the table does not refresh.. – nigel Commented Aug 19, 2013 at 18:11
- just a guess: the response may be cached by the browser. So in your PHP you should add: header("Cache-Control: no-cache,no-store") – oyophant Commented Aug 19, 2013 at 18:21
- A lot of other things to improve here. It's ugly to return HTML - use a data only format like JSON or XML. Maybe one day you want to write an iOS, Andoid app or any other native client. Then the HTML table is pretty useless. And if its HTML, never specify border or style directly - use css. – oyophant Commented Aug 19, 2013 at 18:31
3 Answers
Reset to default 3I think setInterval
with jQuery.load
is the one you're looking for
var table = $("#tableID");
// refresh every 5 seconds
var refresher = setInterval(function() {
table.load("/path/to/js.php");
}, 5000);
Or shorten it up with
var refresher = setInterval(table.load.bind(table, "/path/to/data"), 5000);
If you'd ever like to stop refreshing the data, (e.g.,) say the user leaves the page open for a long time
// stop refreshing after 30 minutes
setTimeout(function() {
clearTimeout(refresher);
}, 1800000);
If your data load takes a while, you might want to only refresh X seconds after the data is loaded. You could do that like this using setTimeout
var table = $("#tableID");
var refresh = function() {
table.load("/path/to/js.php", function() {
setTimeout(refresh, 5000);
});
};
refresh();
Just my 2 cents, but this just seems like an unnecessary load to hit your db with. I would consider something like a trigger and write to something less expensive like a
bool = timestamp > now;
or
if(myhash != tablehash)
So your long polling is just asking if something has changed and not running the query
Change
$('#tableID').replaceWith($(data));
Into
$('#tableID').replaceWith(data);
Although the thing you do is loading some things into an element. So semantically speaking using .load() sounds better.
$('#tableID').load('some/url/file.php')
本文标签: phpAuto Refresh a Html table every x secondsStack Overflow
版权声明:本文标题:php - Auto Refresh a Html table every x seconds - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742247014a2440040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论