php mysql foreach循环重复结果

时间:2014-09-24 16:05:30

标签: php mysql foreach

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

$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@email.com

我在这里搜索过类似的问题,找不到能解决问题的方法。任何想法??

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

2 个答案:

答案 0 :(得分:2)

你的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>";
 }

答案 1 :(得分:2)

您正在错误的地方运行foreach循环 - 您应该将它放在while循环之外。另外 - 为什么要复制许多变量?它可以像这样简单地完成(在一个数组中存储适当的行,而不是5个数组:),如下所示:

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