douyue8191 2015-05-18 16:46
浏览 32

mysql数据被多次显示

I am trying to fetch details from a mysql table and display it using ajax. I have been successful in showing my records but it seems the records are being displayed in a loop. For ex: Numbers from 1 to 10 are being displayed and as soon as the list of 10 is completed, again numbers from 1 to 10 are being displayed. I cant find where I am going wrong.

Javascript

<script type="text/javascript">
$(function(){
    setInterval(function(){
        $.ajax({
            url: 'details.php',
            success: function(data){
                var response=JSON.parse(data);
                var no=1;

                $(function(){
                    $.each(response, function(i, item){
                        var change = no % 2;
                        if(change === 0){
                            $('<tr>').append(
                                $('<td>').text(item.memo).addClass('table1'),
                                $('<td>').text(item.pid).addClass('table1'),
                                $('<td>').text(item.qty).addClass('table1'),
                                $('<td>').text(item.amt).addClass('table1')
                            ).appendTo('#Table1');
                        }
                        else{
                            $('<tr>').append(
                                $('<td>').text(item.memo).addClass('table2'),
                                $('<td>').text(item.pid).addClass('table2'),
                                $('<td>').text(item.qty).addClass('table2'),
                                $('<td>').text(item.amt).addClass('table2')
                            ).appendTo('#Table1');
                        }
                        no = no+1;
                    });
                });
            }
        });
    }, 1000);
});
</script>

PHP

<?php
include 'connect.php';
$timestamp = "";
$return = array();
$response = "";
$sql = "SELECT * FROM receipts ORDER BY timestamp DESC LIMIT 1";
$result1 = mysqli_query($db, $sql);
if($details = mysqli_fetch_array($result1)){
    $timestamp = $details['timestamp'];
}

$sql = "SELECT * FROM sales WHERE timestamp>'$timestamp'";
$result = mysqli_query($db, $sql);
while($fetch_options=mysqli_fetch_array($result)){
    $return[]=array('memo'=>$fetch_options['memo'], 'pid'=>$fetch_options['pid'], 'qty'=>$fetch_options['qty'], 'amt'=>$fetch_options['amt']);
}
$response = $return;
echo json_encode($response);
?>

HTML

     <table width="300" id="Table1" name="Table1">
     <tr>
        <th width="68" id="wb_uid0"><div class="CustomStyle"><span id="wb_uid1"><strong> Memo</strong></span></div>
        </th>
        <th width="64" id="wb_uid2"><div><span id="wb_uid3"><strong> Product ID</strong></span></div>
        </th>
        <th width="50" id="wb_uid4"><div><span id="wb_uid5"><strong> Qty</strong></span></div>
        </th>
        <th width="88" id="wb_uid6"><div><span id="wb_uid7"><strong> Amount</strong></span></div>
        </th>
     </tr>
     </table>
  • 写回答

2条回答 默认 最新

  • dou6495 2015-05-18 16:55
    关注

    you can try like this

    change JS

    <script type="text/javascript">
    $(function(){
         setInterval(function(){
        $.ajax({
            url: 'details.php',
            success: function(data){
    
                $('#Table1').find('#appendRow').remove();
                var response=JSON.parse(data);
                var no=1;
    
                    $.each(response, function(i, item){
                        var change = no % 2;
                        if(change === 0){
                            $('<tr id="appendRow">').append(
                                $('<td>').text(item.memo).addClass('table1'),
                                $('<td>').text(item.pid).addClass('table1'),
                                $('<td>').text(item.qty).addClass('table1'),
                                $('<td>').text(item.amt).addClass('table1')
                            ).appendTo('#Table1');
                        }
                        else{
                            $('<tr id="appendRow">').append(
                                $('<td>').text(item.memo).addClass('table2'),
                                $('<td>').text(item.pid).addClass('table2'),
                                $('<td>').text(item.qty).addClass('table2'),
                                $('<td>').text(item.amt).addClass('table2')
                            ).appendTo('#Table1');
                        }
                        no = no+1;
                });
            }
        });
    }, 1000);
    });
    </script>
    

    I am not sure but i think it's helpful to you.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀