简体   繁体   中英

How to display only once time for duplicated data using foreach in PHP?

I want to select some data from a cross server using PHP foreach and curl by pass it to a table.

I want to query the result of draw two times per day example: from

2015-12-08 13:00 and

2015-12-08 18:00 and I will display it as the correct images 这是正确的结果,它将像这样显示 But what I got from this function is like this 我不知道如何像上面的图像那样玩我的桌子

public function getNumber() {

    $url = "http://103.5.126.24/PalmHallServer_kl/coodraw/coodraw!queryLuckNumberRecordByPeriods.action";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: Json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $contents = curl_exec($ch);
    if($contents){
        echo $contents;
    }else{
        return FALSE;
    }
}

Here is Ajax

 $.get('<?PHP echo base_url('getnumber'); ?>', function (ldata, status) {

            var $json = $.parseJSON(ldata);
            $("#results").html("");

            var result = [];
            $.each(eval($json), function (i, val) {
                if (val.productName == 'DRAW1') {
                    result = val.luckNumber;
                }
                if (val.productName == 'DRAW2') {
                    result = val.luckNumber;
                }
                if (val.productName == 'DRAW3') {
                    result = val.luckNumber;
                }
                if (val.productName == 'DRAW4') {
                    result = val.luckNumber;
                }
                var table = '<td>' + result + '</td>';
                $(table).appendTo('#results');
            });
        });

This is data aft There is alot of result but I selected only a day You can see on this link

[http://103.5.126.24/PalmHallServer_kl/coodraw/coodraw!queryLuckNumberRecordByPeriods.action][3]
[{"betEndtime":null,"betStartime":"2014-01-19 13:00:00","createTime":"2015-12-08 13:00:09","id":100005553,"lotteryOpenStatus":5,"lotteryOpenTime":"2015-12-08 13:00:00","lotteryType":11,"luckNumber":"22522","luckNumberSource":"","operatorName":"auto_open_draw","operatorTeleno":"","orderId":"","periods":"201512081300","prizeNumCode":0,"prizeNumLevel":1,"productId":11111,"productJson":{"baseTime":20140119130000,"commissionRatio":10,"deductBetCount":1,"detail":"","enable":1,"freeResDiscount":1,"lotteryTime":20151208180000,"luckDrawDesc":"DRAW1","luckDrawDiscount":1,"maxLuckNumLen":5,"maxMoney":2000000,"minLuckNumLen":5,"minMoney":100,"nextStartTime":"2015-12-08 13:00:00","nextStopTime":"2015-12-08 17:45:00","openDrawType":0,"periodUnit":1,"periodUnitNum":1,"price":40,"productCode":"DRAW1","productDesc":"Draw1","productEndTime":20990119163000,"productId":11111,"productType":11,"rate":0,"resTmplProductId":11111,"stopTime":900,"validPeriod":2},"productName":"DRAW1","publicTime":null,"rewardMultiple":0,"updateTime":"2015-12-08 13:01:22","winAmount":0},{"betEndtime":null,"betStartime":"2014-01-19 13:00:00","createTime":"2015-12-08 13:00:15","id":100005554,"lotteryOpenStatus":5,"lotteryOpenTime":"2015-12-08 13:00:00","lotteryType":11,"luckNumber":"29848","luckNumberSource":"","operatorName":"auto_open_draw","operatorTeleno":"","orderId":"","periods":"201512081300","prizeNumCode":0,"prizeNumLevel":1,"productId":11112,"productJson":{"baseTime":20140119130000,"commissionRatio":10,"deductBetCount":1,"detail":"","enable":1,"freeResDiscount":1,"lotteryTime":20151208180000,"luckDrawDesc":"DRAW2","luckDrawDiscount":1,"maxLuckNumLen":5,"maxMoney":2000000,"minLuckNumLen":5,"minMoney":100,"nextStartTime":"2015-12-08 13:00:00","nextStopTime":"2015-12-08 17:45:00","openDrawType":0,"periodUnit":1,"periodUnitNum":1,"price":40,"productCode":"DRAW2","productDesc":"Draw2","productEndTime":20990119163000,"productId":11112,"productType":11,"rate":0,"resTmplProductId":11112,"stopTime":900,"validPeriod":2},"productName":"DRAW2","publicTime":null,"rewardMultiple":0,"updateTime":"2015-12-08 13:02:32","winAmount":0},{"betEndtime":null,"betStartime":"2014-01-19 13:00:00","createTime":"2015-12-08 13:00:18","id":100005555,"lotteryOpenStatus":5,"lotteryOpenTime":"2015-12-08 13:00:00","lotteryType":11,"luckNumber":"35495","luckNumberSource":"","operatorName":"auto_open_draw","operatorTeleno":"","orderId":"","periods":"201512081300","prizeNumCode":0,"prizeNumLevel":1,"productId":11113,"productJson":{"baseTime":20140119130000,"commissionRatio":10,"deductBetCount":1,"detail":"","enable":1,"freeResDiscount":1,"lotteryTime":20151208180000,"luckDrawDesc":"DRAW3","luckDrawDiscount":1,"maxLuckNumLen":5,"maxMoney":2000000,"minLuckNumLen":5,"minMoney":100,"nextStartTime":"2015-12-08 13:00:00","nextStopTime":"2015-12-08 17:45:00","openDrawType":0,"periodUnit":1,"periodUnitNum":1,"price":40,"productCode":"DRAW3","productDesc":"Draw3","productEndTime":20990119163000,"productId":11113,"productType":11,"rate":0,"resTmplProductId":11113,"stopTime":900,"validPeriod":2},"productName":"DRAW3","publicTime":null,"rewardMultiple":0,"updateTime":"2015-12-08 13:03:27","winAmount":0},{"betEndtime":null,"betStartime":"2014-01-19 13:00:00","createTime":"2015-12-08 13:00:21","id":100005556,"lotteryOpenStatus":5,"lotteryOpenTime":"2015-12-08 13:00:00","lotteryType":11,"luckNumber":"89346","luckNumberSource":"","operatorName":"auto_open_draw","operatorTeleno":"","orderId":"","periods":"201512081300","prizeNumCode":0,"prizeNumLevel":1,"productId":11114,"productJson":{"baseTime":20140119130000,"commissionRatio":10,"deductBetCount":1,"detail":"","enable":1,"freeResDiscount":1,"lotteryTime":20151208180000,"luckDrawDesc":"DRAW4","luckDrawDiscount":1,"maxLuckNumLen":5,"maxMoney":2000000,"minLuckNumLen":5,"minMoney":100,"nextStartTime":"2015-12-08 13:00:00","nextStopTime":"2015-12-08 17:45:00","openDrawType":0,"periodUnit":1,"periodUnitNum":1,"price":40,"productCode":"DRAW4","productDesc":"Draw4","productEndTime":20990119163000,"productId":11114,"productType":11,"rate":0,"resTmplProductId":11114,"stopTime":900,"validPeriod":2},"productName":"DRAW4","publicTime":null,"rewardMultiple":0,"updateTime":"2015-12-08 13:04:26","winAmount":0}]

Change the AJAX code to following:

 $("#results").html(""); var result = []; var table = ''; tmpDRAW1 = ''; tmpDRAW2 = ''; tmpDRAW3 = ''; tmpDRAW4 = ''; tmpCounter = 0; $.each(eval($json), function(i, val) { tmpOpenTime = val.lotteryOpenTime; if (val.productName == 'DRAW1') { tmpDRAW1 = val.luckNumber; tmpCounter++; } if (val.productName == 'DRAW2') { tmpDRAW2 = val.luckNumber; tmpCounter++; } if (val.productName == 'DRAW3') { tmpDRAW3 = val.luckNumber; tmpCounter++; } if (val.productName == 'DRAW4') { tmpDRAW4 = val.luckNumber; tmpCounter++; } if (tmpCounter == 4) { table += '<tr><td>' + tmpOpenTime + '</td>'; table += '<td>' + tmpDRAW1 + '</td>'; table += '<td>' + tmpDRAW2 + '</td>'; table += '<td>' + tmpDRAW3 + '</td>'; table += '<td>' + tmpDRAW4 + '</td></tr>'; tmpCounter = 0; } }); $(table).appendTo('#results'); }); 

This does the output format you want, and is resistant to ordering of DRAWs in your JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM