如何使javascript在SQL while循环中回显所有数据

How do you make javascript echo all the data whilst in a SQL while loop?

本文关键字:数据 循环 javascript 何使 SQL while      更新时间:2023-09-26

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

<?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>';
}
?>

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

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

话虽如此,解决这个问题的最佳方法是,您应该将数据作为每行特定元素的数据属性存储在HTML中,用JavaScript检索,然后启动一个超时,同时更新所有元素。