PHP while 循环在 continue

问题描述

我的脚本有问题。当我的 $sended 变量设置为 0 时,我需要发送邮件,如果设置为 1,则我需要忽略此声明

<?PHP
while ($result = $pdo->fetch(PDO::FETCH_ASSOC)) {

        if($result['howMany'] == "0") {
          if ($sended == "0") {
            //send mail to admin with info that product was end.
            //this is work fine
            if($mail->send()) {
              //change in my database variable $sended to 1;
              //this is work fine to.
              //if i live it empty,it is work,but if I change $sended to 1,it is not working
            }
          }
          // if variable $result['howMany'] is equal to 0 than i need to end this iteration of while loop.
          continue;
        }

        //rest of script that should work when $result['howMany'] != "0"
}
?>

问题是当 $sended 变量设置为 0 时,continue 不仅结束了这一次迭代,而且结束了整个循环。当 $sended == 1 时,一切正常。继续正常工作。我做错了什么?错误在哪里,因为我没有看到它。对不起我的英语不好。这不是我的母语。

解决方法

你应该这样使用 if-else 语句:

while ($result = $pdo->fetch(PDO::FETCH_ASSOC)) {
$temp = false;
if($result['howMany'] == "0") {    
    if ($sended == "0") {
        //send mail to admin with info that product was end.
        //this is work fine
        if($mail->send()) {
          //change in my database variable $sended to 1;
          //this is work fine to.
        }
      }
     else
      {
        $temp = true;
      }
    }
 // use outside all if-else but inside loop
 $temp == true ? continue : false;
}
,

$sended 未在您的代码中定义。

您可以直接选择以查看哪些库存是空的(WHERE howMany = 0),然后创建电子邮件并发送,而不是选择所有库存然后进行检查循环。

或者,如果你想像你正在做的那样做,设置一个标志来发送,建立电子邮件,然后在循环结束后发送,它会让你确定你不建立电子邮件正文'如果一封电子邮件就足够了,我不想为每件缺货的商品发送一封电子邮件。

<?php
$pdo = new class {
    public $rows = [
        [
            'id' => 1,'Product' => 'Toilet Rolls','howMany' => 0,'sended' => 0
        ],[
            'id' => 2,'Product' => 'Baked Beans','howMany' => 7,[
            'id' => 3,'Product' => 'Sugar Puffs','sended' => 1
        ],];
    
    public function fetch() {
        return array_pop($this->rows);
    }
};

$mail = new class {
    public $to = '';
    public $subject = '';
    public $body = '';
    public $debug = false;
    
    public function send() {
        if ($this->debug) echo "Sending email:\nTo: {$this->to}\nSubject: {$this->subject}\nBody: {$this->body}".PHP_EOL;
        
        return true;
    }
};


$stockEmail = [
  'body' => '','shouldSend' => false    
];

while ($result = $pdo->fetch(PDO::FETCH_ASSOC)) {

    // got stock with no items and not sended
    if (!$result['howMany'] && !$result['sended']) {
        
        // flag to send
        $stockEmail['shouldSend'] = true;
        
        // email body (header)
        if (empty($stockEmail['body'])) {
            $stockEmail['body'] .= 'Hello,Mr Stockman!'.PHP_EOL;
            $stockEmail['body'] .= 'The following stock is empty,re-ordering is required:'.PHP_EOL;
        }
        
        // email body (continuation)
        $stockEmail['body'] .= ' - '.$result['Product'].PHP_EOL;
        
        // do database update with $result['id']
        // UPDATE stock SET sended = 1 WHERE id = ?
    }
}

// has stock email
if ($stockEmail['shouldSend']) {
    $mail->debug = true;
    $mail->to = 'stockman@acme.company.example.com';
    $mail->from = 'stockcontrol@acme.company.example.com';
    $mail->subject = 'Stock control message';
    $mail->body = $stockEmail['body'];
    
    
    if ($mail->send()) {
        echo 'Stock control email sent!';
    } else {
        echo 'Error sending stock control email!';
    }
}

结果:

Sending email:
To: stockman@acme.company.example.com
Subject: Stock control message
Body: Hello,Mr Stockman!
The following stock is empty,re-ordering is required:
 - Toilet Rolls

Stock control email sent!