PHP while循环中的Javascript倒计时

时间:2016-01-27 13:38:18

标签: javascript php timer countdown

编辑:已解决。

'items'表有多个元组,对于每个元组,我曾经有过要在网页上打印的名称,图像,价格,描述和投标系统(文本字段和提交)。 它确实很有效,直到我决定我们每个人都想要倒数计时器。 此计时器以这种方式工作:每次对某个项目进行新的出价时,该项目的计时器将重置为X分钟/秒/无论如何。 当我将include.php插入原始代码时,只处理第一个实例。

我知道这是因为PHP和Javascript不能同时“运行”,但我想知道如何修复我的Javascript来解决问题。

经典的回声解决方案不起作用。

这是while循环:

while ($row = @ mysql_fetch_array($results))
{

print
"<div id='item'>".
"<p>".$row["name"]."</p>".
"<img src= uploads/".$row["image"].">".
"<p>&euro;".$row["final_price"]."</p>".
"<p>".$row["description"]."</p>".
"<form method = 'get' action = 'sum.php'>".
"<input type = 'hidden' name = 'itid' value = ".$row["id"].">".
"<tr><td><input type = 'number' name = 'bid' required = 'required'></td></tr><br>".
"<tr><td><input type = 'submit' value = 'Bid'></td></tr>".
"</form>";
include('cntdwn.php');
print
"</div>";
}

通过这样设置cntdwn.php解决:

<?php
${"query1".$row['id']}= "SELECT time_up FROM items WHERE id = '" .$row["id"]."'";
${"results".$row['id']} = mysql_query (${"query1".$row['id']});
while (${"row".$row['id']} = @ mysql_fetch_array(${"results".$row['id']})) {${"rez".$row['id']} = ${"row".$row['id']}['time_up'];}
//$date = 'January 22 2017 09:10:00 PM CET';
${"exp_date".$row['id']} = strtotime(${"rez".$row['id']});
${"now".$row['id']} = time();

if (${"now".$row['id']} < ${"exp_date".$row['id']}) {
?>
<script>
// Count down milliseconds = server_end - server_now = client_end - client_now
var server_end<?php echo $row['id']; ?> = <?php echo ${"exp_date".$row['id']}; ?> * 1000;
var server_now<?php echo $row['id']; ?> = <?php echo time(); ?> * 1000;
var client_now<?php echo $row['id']; ?> = new Date().getTime();
var end<?php echo $row['id']; ?> = server_end<?php echo $row['id']; ?> - server_now<?php echo $row['id']; ?> + client_now<?php echo $row['id']; ?>; // this is the real end time

var _second<?php echo $row['id']; ?> = 1000;
var _minute<?php echo $row['id']; ?> = _second<?php echo $row['id']; ?> * 60;
var _hour<?php echo $row['id']; ?> = _minute<?php echo $row['id']; ?> * 60;
var _day<?php echo $row['id']; ?> = _hour<?php echo $row['id']; ?> *24;
var timer<?php echo $row['id']; ?>;

function showRemaining<?php echo $row['id']; ?>()
{
    var now<?php echo $row['id']; ?> = new Date();
    var distance<?php echo $row['id']; ?> = end<?php echo $row['id']; ?> - now<?php echo $row['id']; ?>;
    if (distance<?php echo $row['id']; ?> < 0 ) {
       clearInterval( timer<?php echo $row['id']; ?> );
       location.reload();

       return;
    }
    var days<?php echo $row['id']; ?> = Math.floor(distance<?php echo $row['id']; ?> / _day<?php echo $row['id']; ?>);
    var hours<?php echo $row['id']; ?> = Math.floor( (distance<?php echo $row['id']; ?> % _day<?php echo $row['id']; ?> ) / _hour<?php echo $row['id']; ?> );
    var minutes<?php echo $row['id']; ?> = Math.floor( (distance<?php echo $row['id']; ?> % _hour<?php echo $row['id']; ?>) / _minute<?php echo $row['id']; ?> );
    var seconds<?php echo $row['id']; ?> = Math.floor( (distance<?php echo $row['id']; ?> % _minute<?php echo $row['id']; ?>) / _second<?php echo $row['id']; ?> );

    var countdown<?php echo $row['id']; ?> = document.getElementById('countdown<?php echo $row['id']; ?>');
    countdown<?php echo $row['id']; ?>.innerHTML = '';
    if (days<?php echo $row['id']; ?>) {
        countdown<?php echo $row['id']; ?>.innerHTML += 'Days: ' + days<?php echo $row['id']; ?> + '<br />';
    }
    countdown<?php echo $row['id']; ?>.innerHTML += 'Hours: ' + hours<?php echo $row['id']; ?>+ '<br />';
    countdown<?php echo $row['id']; ?>.innerHTML += 'Minutes: ' + minutes<?php echo $row['id']; ?>+ '<br />';
    countdown<?php echo $row['id']; ?>.innerHTML += 'Seconds: ' + seconds<?php echo $row['id']; ?>+ '<br />';
}

timer<?php echo $row['id']; ?> = setInterval(showRemaining<?php echo $row['id']; ?>, 1000);
</script>
<?php
}
else {
    ${"winnerquery".$row['id']}= "SELECT u_mail FROM bids WHERE datestamp = (SELECT MAX(datestamp) FROM bids) AND i_id = '".$row['id']."' LIMIT 1";
    ${"rezul".$row['id']} = mysql_query(${"winnerquery".$row['id']});
    while (${"rol".$row['id']} = mysql_fetch_array(${"rezul".$row['id']})) { ${"rel".$row['id']} = ${"rol".$row['id']}['u_mail'];}
    ${"relative".$row['id']} = mysql_query("UPDATE items SET sold = '1',sold_to = '".${"rel".$row['id']}."' WHERE id = '".$row['id']."'");
 echo "Times Up";


}
?>
<div id="countdown<?php echo $row['id']; ?>"></div>

0 个答案:

没有答案