gpt4 book ai didi

php mysql foreach循环重复结果

转载 作者:行者123 更新时间:2023-11-29 04:42:12 26 4
gpt4 key购买 nike

这是 while 循环中 if 中的复杂 foreach,出于某种原因,它会在每次运行循环时复制结果并附加它们。代码和输出如下:

$timezonedate = date('Y-m-d');
$yesterday = date('Y-m-d', strtotime( '-1 days' ));
$tomorrow = date('Y-m-d', strtotime( '+1 days' ));

$timezonesql = "SELECT contacts.id AS contact_id, contacts.cbdate AS cbdate, contacts.cbtime AS cbtime, contacts.firstName AS contact_firstName, contacts.lastName AS contact_lastName, contacts.email AS contact_email, contacts.tel1 AS tel, contacts.rep AS contact_rep, members.id AS member_id, members.firstName AS member_firstName, members.lastName AS member_lastName, members.email AS member_email, members.timezone AS timezone FROM contacts INNER JOIN members ON contacts.rep = members.id WHERE contacts.cbdate = '$timezonedate' || contacts.cbdate = '$yesterday' || contacts.cbdate = '$tomorrow' ORDER BY contacts.id ASC";

$timezoneresult = mysql_query($timezonesql, $link);

if(mysql_num_rows($timezoneresult) == 0) {

}

else

{

while($timezoneRow = mysql_fetch_array($timezoneresult)) {

date_default_timezone_set($timezoneRow['timezone']);

$nowdate = date('Y-m-d');
$beforetime = date('H:i:59', time() - 1*60);
$aftertime = date('H:i:00', time() + 1*60);

if($timezoneRow['cbdate'] = $nowdate && $timezoneRow['cbtime'] > $beforetime && $timezoneRow['cbtime'] < $aftertime) {

$contactid[] = $timezoneRow['contact_id'];
$contactemail[] = $timezoneRow['contact_email'];
$contactfirstName[] = $timezoneRow['contact_firstName'];
$contactlastName[] = $timezoneRow['contact_lastName'];
$memberemail[] = $timezoneRow['member_email'];

foreach($contactid as $key=>$val) {
echo "".$contactfirstName[$key]." ".$contactlastName[$key]." ".$memberemail[$key]."<br>";
}
}
else {}
}
}
exit;

输出:

mickey mouse mickey@email.com

mickey mouse mickey@email.com
minnie mouse minnie@email.com

mickey mouse mickey@email.com
minnie mouse minnie@email.com
donald duck donald@email.com

mickey mouse mickey@email.com
minnie mouse minnie@email.com
donald duck donald@email.com
goofy dog goofy@email.com

我在这里搜索了类似的问题,但找不到可以解决我的问题的问题。有什么想法吗??

免责声明:我知道我应该使用准备好的语句,我会在这个项目完成后立即开始。

最佳答案

您的 foreach 循环应该在 while 循环之外:

while($timezoneRow = mysql_fetch_array($timezoneresult)) {

date_default_timezone_set($timezoneRow['timezone']);

$nowdate = date('Y-m-d');
$beforetime = date('H:i:59', time() - 1*60);
$aftertime = date('H:i:00', time() + 1*60);

if($timezoneRow['cbdate'] = $nowdate && $timezoneRow['cbtime'] > $beforetime && $timezoneRow['cbtime'] < $aftertime) {

$contactid[] = $timezoneRow['contact_id'];
$contactemail[] = $timezoneRow['contact_email'];
$contactfirstName[] = $timezoneRow['contact_firstName'];
$contactlastName[] = $timezoneRow['contact_lastName'];
$memberemail[] = $timezoneRow['member_email'];

}
else {}
}

foreach($contactid as $key=>$val) {
echo "".$contactfirstName[$key]." ".$contactlastName[$key]." ".$memberemail[$key]."<br>";
}

关于php mysql foreach循环重复结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26021567/

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