This is a complicated foreach within an if within a while loop and for some reason it is duplicating the results and appending them each time it runs the loop. The code and output are as follows:
$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;
output:
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.commickey mouse mickey@email.com
minnie mouse minnie@email.com
donald duck donald@email.com
goofy dog goofy@email.com
I have searched through the similar questions on here and could not find one that made sense to fix my problem. Any ideas??
Disclaimer: I know I should be using prepared statements and I will begin as soon as this project is finished.