php uses NOT operator to evaluate conditions
P粉618358260
P粉618358260 2024-04-02 09:57:41
0
1
461

I have 4 sales offices and once all 4 sales offices have received 20 leads, only sales office number 4 should receive the remaining leads for the day. I need some help creating a condition to basically check if sales offices 1-4 have each received 20 leads and then obviously follow the instructions to proceed

First I query the database to check my counter (yes I know this can be more compact and done inside a loop)

$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'];

When I execute this logic, it doesn't work as expected? Once sales_office = 1 reaches 20 leads it will be transferred directly to sales office 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
}

P粉618358260
P粉618358260

reply all(1)
P粉792673958

Your current logic check for all office counts, you must check each office individually.

if ($sales_office1_count 

Additionally, you can improve the readability of your code by using arrays:

$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] 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template