I have 4 sales offices, as soon as all 4 sales offices have received 20 leads each, only sales office no 4 should then receive the rest of the leads for the day. I need some help with creating the condition to basically check if sales office 1-4 have each received 20 leads and to then obviously move on as explained
Firstly I query the database to check my counter(yes I know this can be more compact and done within a loop instead)
$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 does not work as intended? As soon as sales_office = 1 reaches 20 leads it then moves straight 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
}
CodePudding user response:
Your current logic check for all office count you have to check each of them separately.
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
}
Also, you can improve your code readability 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] < 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
}