php - php 使用 NOT 运算符评估条件

我有 4 个销售办事处,一旦所有 4 个销售办事处每个都收到 20 条线索,那么只有 4 号销售办事处应该收到当天剩余的线索。我需要一些帮助来创建条件以基本上检查销售办公室 1-4 是否每个都收到了 20 个潜在客户,然后显然按照说明继续

首先,我查询数据库以检查我的计数器(是的,我知道这可以更紧凑,而是在循环中完成)

$sales_office= 1;
$sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
$sql->execute([$sales_office]);
$result = $sql->fetch();
$sales_office1_count = $result['LeadsReceivedToday'];

$sales_office= 2;
$sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
$sql->execute([$sales_office]);
$result = $sql->fetch();
$sales_office2_count = $result['LeadsReceivedToday'];

$sales_office= 3;
$sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
$sql->execute([$sales_office]);
$result = $sql->fetch();
$sales_office3_count = $result['LeadsReceivedToday'];

$sales_office= 4;
$sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
$sql->execute([$sales_office]);
$result = $sql->fetch();
$sales_office4_count = $result['LeadsReceivedToday'];

当我执行此逻辑时,它没有按预期工作?只要 sales_office = 1 达到 20 个潜在客户,它就直接移动到销售办公室 4?

if ($sales_office1_count != 20 && $sales_office2_count != 20 && $sales_office3_count != 20)
{
    //distribute leads to sales office 1-4 in order of 1-4
    
}
else
{
   //only send leads to sales office no 4
}

回答1

您当前对所有办公室计数的逻辑检查,您必须分别检查它们中的每一个。

if ($sales_office1_count < 20 || $sales_office2_count < 20 || $sales_office3_count < 20)
{
    //distribute leads to sales office 1-4 in order of 1-4
    
}
else
{
   //only send leads to sales office no 4
}

此外,您可以通过使用数组来提高代码的可读性:

$officesLeadsCount = [1 => 0, 2 => 0, 3 => 0, 4 => 0];

foreach ($officesLeadsCount as $number => $value) {
  $sql = $conn->prepare("SELECT LeadsReceivedToday FROM Lead_Counter WHERE SalesOffice=?");
  $sql->execute([$number]);
  $result = $sql->fetch();
  $officesLeadsCount[$number] = $result['LeadsReceivedToday'];    
}


if ($officesLeadsCount[1] < 20 || $officesLeadsCount[2] < 20 || $officesLeadsCount[3] < 20)
{
    //distribute leads to sales office 1-4 in order of 1-4

}
else
{
   //only send leads to sales office no 4
}

相似文章