首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让javascript在SQL while循环中回显所有数据?

如何让javascript在SQL while循环中回显所有数据?
EN

Stack Overflow用户
提问于 2015-03-18 05:42:05
回答 1查看 129关注 0票数 0

因此,我尝试为我的脚本做一个计时器,但它只对最后一个结果进行倒计时,而不是对所有结果进行倒计时,并且对于从查询返回的所有结果,都显示了从最后一个结果开始的倒计时。任何帮助都将不胜感激。谢谢

代码语言:javascript
复制
<?php 
$q = $handler->prepare("SELECT * FROM pending_payments WHERE user_id = ? AND status = ?");
$q->bindParam(1, $session_id);
$q->bindParam(2, $pending);
$q->execute();
if($q->rowCount() > null){
?>                      
                        <h4 class="title"> Pending Payment </h4>
                        <div class="squiggly-border"></div>
                        <table class="table table-index">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Item</th>
                                    <th>Expires</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
<?php
while($r = $q->fetch()) {
$pending_payment_id = $r['id'];
$pending_item = $r['item'];
$pending_expiry = $r['expiry'];
?>
<script type="text/javascript">
var count = <?php if($dateUNIX < $pending_expiry){ echo $pending_expiry - $dateUNIX; } ?>;
var counter = setInterval(timer, 1000); //1000 will  run it every 1 second

function timer() {
    count = count - 1;
    if (count == -1) {
        clearInterval(counter);
        return;
    }

    var seconds = count % 60;
    var minutes = Math.floor(count / 60);
    var hours = Math.floor(minutes / 60);
    minutes %= 60;
    hours %= 60;

    document.getElementById("pEFTimer<?php echo $r['id'] ?>").innerHTML = hours + " hours " + minutes + " minutes "; // watch for spelling
}
</script>                           
                                <tr>
                                    <td class="text"><?php echo $pending_payment_id; ?></td>
                                    <td class="text"><?php echo $pending_item ?></td>
                                    <td class="text" id="pEFTimer<?php echo $r['id'] ?>"></td>
                                    <td class="text"><a href="pendingPayment<?php echo $r['id']; ?>" ><button class="btn btn-success btn-xs">Proceed to Payment</button></a></td>
                                </tr>
<?php
}
echo '</tbody></table>';
}
?>
EN

回答 1

Stack Overflow用户

发布于 2015-03-18 05:52:25

在javascript中,函数是执行某些代码的变量,所以基本上不是为每一行创建一个函数,而是将函数更新为只跟踪最后一个元素。

让我们弄清楚每种语言工作的地方,PHP在服务器中输出您的html,html由浏览器解析,然后Javascript在浏览器(客户端)中执行。

也就是说,解决这个问题的最好方法是将数据作为数据属性存储在超文本标记语言中的每一行的特定元素上,使用JavaScript进行检索,然后启动一个同时更新所有元素的超时。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29110259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档