我目前被困在一个问题上,不确定如何解决它。我是 php 和 javascript 编码的新手,所以如果有更简单的方法,请纠正我。
我正在制作一个足球运动员拍卖网站。它就像 ebay,只是它是播放器而不是元素。基本上,您将玩家出售,并且会有一个倒数计时器。
我的问题是我正在尝试让一个倒数计时器为多个玩家工作。我已经制作了一个成功的倒计时功能,它只适用于一个玩家,但是当我尝试对多个玩家进行时,它并没有按预期工作。它只更新最后一个玩家。
这是一个玩家的输出。
Player Name: Rooney
Hours:20 Minutes:16 Seconds:56
这是两个或更多玩家的输出
Player Name: Messi
Player Name: Beckham
Player Name: Rooney
Hours:20 Minutes:16 Seconds:56
我将解释到目前为止我得到了什么。
包含玩家姓名和结束时间的数据库。一个用于连接数据库的 php 文件。最后一个文件包含我的倒计时脚本和我正在测试的地方。
数据库结构
|Player Name|------------|End date/time|---------
Messi July 31 2015 05:00:00 PM
Ronaldo July 31 2015 10:00:00 PM
请注意,结束日期时间是一个字符串,格式如上所示,因此我可以使用 strtotime 函数检索剩余时间。
这就是我正常 body 的运作方式。我使用 PHP 和 MySQL 选择了上面的表格。然后我循环打印出每个玩家的名字。在打印每个玩家名字的过程中,我调用计时器函数来获取倒计时时间。该函数接受两个参数。首先是结束日期时间。第二个参数是唯一的 id(div 标签)。我从数据库中获取结束日期/时间,并使用数组创建一个唯一的 id 标签。
我的意图是创建一个函数,它获取结束日期和玩家的唯一 ID,然后像这样编辑它
document.getElementById(uniqueID).innerHTML =...
因为这是在我知道的 div 中编辑内容的唯一方法,而且我正在循环创建多个 div。
这是带有一些注释的主脚本
//Connecting to database
<?php
include 'connect.php';
?>
<!doctype html>
<html>
<head>
<?php
//array used to create unique id for our divs
$ids = array("one","two","three","four","five","six","seven","eight","nine");
$i =0;
function timer($date, $id){
//changing time to london(uk) time.
date_default_timezone_set("Europe/London");
//converting the string from database into time form
$endtime = strtotime($date);
//Returns the current time
$idtemp = $id;
?>
<script>
//convert server time to milliseconds
var server_current = <?php echo time(); ?> * 1000;
var server_end_time = <?php echo $endtime; ?> * 1000;
var client_current_time = new Date().getTime();
//server end time - server current time + client current time
var finish_time = server_end_time - server_current + client_current_time;
var timer;
var uniqueID = <?php echo json_encode($idtemp); ?> ;
function countdown()
{
var now = new Date();
var left = finish_time - now;
//Following code convert the remaining milliseconds into hours, minutes and days.
//milliseconds conversion
//1000-second 60,000-minute
//3,600,000-hour 86,400,400-hour
var hour = Math.floor( (left % 86000000 ) / 3600000 );
var minute = Math.floor( (left % 3600000) / 60000 );
var second = Math.floor( (left % 60000) / 1000 );
document.getElementById(uniqueID).innerHTML = "Hours:"+hour+" Minutes:"+minute+" Seconds:"+second;
}
timer = setInterval(countdown, 1000);
</script>
<?php }?>
</head>
<body>
<h3>Auction House </h3>
<?php
//select table from database
$result = mysql_query("SELECT * FROM `current auction`");
while($row = mysql_fetch_array($result)){
echo 'Player Name: '. $row['PlayerName'];
$timeleft = $row['Time'];
echo '<div id="c">';
$temp = $ids[$i];
?>
<script>
//Change the div id to the next element in array
document.getElementById("c").setAttribute("id",<?php echo json_encode($temp); ?>);
</script>
<?php
$i = $i +1;
echo $temp;
timer($timeleft, $temp);
echo '</div>';
echo "<br />";
}
?>
</body>
</html>
我不确定为什么它不起作用。
谢谢
最佳答案
我今天感觉很慷慨,所以我让这对你来说很容易。这是我想出的:
<html>
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h3>Auction House </h3>
<table id="table-auctions" class="table table-bordered table-hover table-striped">
<tr>
<th>Name</th>
<th>Expiring</th>
</tr>
</table>
</div> <!-- /container -->
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$(function() {
var times = [
{
'id': '1',
'name': 'Messi',
'end': new Date('July 31, 2015 13:00:00'),
},
{
'id': '2',
'name': 'Ronaldo',
'end': new Date('August 1, 2015 10:00:00'),
},
{
'id': '3',
'player': 'JJ',
'end': new Date('August 7, 2015 13:00:00'),
},
];
// Initialize the table values
$.each(times, function( key, value ) {
$('#table-auctions').append('<tr><td>'+value.name+'</td><td><span id="player-'+value.id+'-expiration" class="label label-primary">Loading...</span></td></tr>');
});
function countdown()
{
var now = new Date();
console.log('updating time');
$.each(times, function( key, value ) {
var left = value.end - now;
var days = Math.floor( left / (1000 * 60 * 60 * 24) );
var hours = Math.floor( (left % (1000 * 60 * 60 * 24) ) / (1000 * 60 * 60) );
var minutes = Math.floor( (left % (1000 * 60 * 60)) / (1000 * 60) );
var seconds = Math.floor( (left % (1000 * 60)) / 1000 );
displayTime = '';
if (days > 0) {
displayTime = "Days: " + days;
}
displayTime = displayTime + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds;
$('#player-'+value.id+'-expiration').text(displayTime)
});
}
timer = setInterval(countdown, 1000);
});
</script>
</body>
</html>
如果您只是将该代码复制并粘贴到它自己的 html 文件中,它本身会很好地工作,您将得到如下内容:
唯一剩下要做的就是将它绑定(bind)到数据库,最好的方法是在 javascript 的 var times =
部分插入一个 foreach 循环。
请注意,您确实应该使用 PHP mysql_query 以外的其他东西,因为它允许各种令人头疼的问题,主要是 SQL 注入(inject)。 Here's a post about an easy way to implement PDO (这是查询数据库的当前正确方法)。
关于javascript - 使用 PHP 和 Javascript 的多次拍卖倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31732531/