Reputation: 121
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 [email protected]
mickey mouse [email protected]
minnie mouse [email protected]
mickey mouse [email protected]
minnie mouse [email protected]
donald duck [email protected]mickey mouse [email protected]
minnie mouse [email protected]
donald duck [email protected]
goofy dog [email protected]
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.
Upvotes: 1
Views: 272
Reputation: 2668
You are running your foreach loop in the wrong place - you should put it outside the while
loop. Plus - why are you duplicating many variables? It could be simply done like this (storing appropriate rows in one array, not 5 arrays :), like this:
$contacts = array();
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) {
$contacts[] = $timezoneRow; // this is enough!
}
else {}
} // end while loop
//print desired data
foreach($contacts as $key=>$contactData) {
echo "".$contactData['contact_firstName']." ".$contactData['contact_lastName']." ".$memberemail['member_email']."<br>";
}
Upvotes: 2
Reputation: 4305
Your foreach loop should be outside of the while loop:
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>";
}
Upvotes: 2