gpt4 book ai didi

javascript - 如何在PHP中使用foreach只显示一次重复的数据?

转载 作者:行者123 更新时间:2023-11-27 23:35:34 25 4
gpt4 key购买 nike

我想使用 PHP foreachcurl 从跨服务器中选择一些数据,并将其传递到表中。

我想查询每天两次抽奖的结果示例:来自

2015-12-08 13:00

2015-12-08 18:00我会将其显示为正确的图像 This is the correct result it will display like this 但我从这个函数得到的是这样的 I don't know how to play my table as above images

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

这是 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');
});
});

这是后面的数据结果有很多,但我只选了一天您可以在此链接上看到

[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}]

最佳答案

将 AJAX 代码更改为以下内容:

$("#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');
});

这可以实现您想要的输出格式,并且不受 JSON 中 DRAW 排序的影响。

关于javascript - 如何在PHP中使用foreach只显示一次重复的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34162115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com