1

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.com

mickey 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.

Paige Rose Figueira
  • 121
  • 1
  • 4
  • 17
  • @DonCallisto the contact_id and the member_id are unique. – Paige Rose Figueira Sep 24 '14 at 16:10
  • Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 24 '14 at 16:13
  • 2
    Even if you add a disclaimer in your question, you can't stop a mysql hater. Ever. – Aycan Yaşıt Sep 24 '14 at 17:47

2 Answers2

2

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>";
 }
David Jones
  • 4,275
  • 6
  • 27
  • 51
2

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>";
}
Kleskowy
  • 2,648
  • 1
  • 16
  • 19