Excluding person from array if already selected for job

Hi, this might be a pretty confusing problem.
But I’m stuck with some infinite loops :eek:

[PHP]function setStaging($boardpoint,$task) {
$date = date(“dmY”);
$this->getRandomPerson($boardpoint,$task,$n);
return true;
}

//Run the function to get a random person from database

function getRandomPerson($boardpoint,$task,$n) {
$sql_text = “SELECT id, name, $task FROM “.$this->TABLES[‘staff’].” WHERE $task = ‘yes’”;
$data = $this->sql->Select($sql_text);
$i=0;
$len = count($data);
$random_number = rand(1,$len);
$array = array(0 => “holder”);
for ($i=0; $i< $len; $i++) {
array_push($array, $data[$i][‘name’]);
}
$person = $array[$random_number];

$this->addPersonToBackup($boardpoint,$person,$task,$n);
return true;

}

//Built an array with all the people who can do the “staging job”
//Call function to add person to database.

function addPersonToBackup($boardpoint,$person,$task,$n) {
$date = date(“dmY”);
$sql_text = “SELECT id, date FROM “.$this->TABLES[‘backup’].” WHERE date = ‘$date’”;
$data = $this->sql->Select($sql_text);
$i=0;
$len = count($data);
if ($task == “fastpost”){
$holder = $task.$n;
}else{
$holder = $task.$boardpoint;
}

	if ($data[$i]['date'] !="") {
	$this->sql->Update("UPDATE ".$this->TABLES['backup']." SET $holder = '".$person."' WHERE date = $date");
}else{
	$this->sql->Insert("INSERT INTO ".$this->TABLES['backup']." ($holder, date) VALUES ('".$person."','".$date."')");

}

if ($task == “staging”){
$this->stagingChecker($boardpoint,$person,$task,$n);
}
return true;
}

//Have to check to see if the person is already allocated to another job before continuing.

function stagingChecker($boardpoint,$person,$task,$n){
$date = date(“dmY”);
$sql_text = “SELECT id, date, fastpost1, fastpost2, fastpost3 FROM “.$this->TABLES[‘backup’].” WHERE date = ‘$date’”;
$data = $this->sql->Select($sql_text);
$len = count($data);
$i=0;
if ($data[$i][‘fastpost1’] == $person || $data[$i][‘fastpost2’] == $person || $data[$i][‘fastpost3’] == $person) {
$this->getRandomPerson($boardpoint,$task,$n);
}else{

				return true;
		  } 

}
//Checks to see if person is set to fastpost. If they aren’t. All is fine, if they are, go back and pick another person.
//People cant do both jobs at once.
[/PHP]

The issue I have is I have more jobs that get checked. As it goes down the list checking, it allocates more and more people. When it gets to the final job for the hour there are still a lot of people going into the array who already have a job to do.
How do I get them out of the array?
Because its randomly picking them, it can pick people who already are on a job. Infinite loop there.

But if it only builds an array with people who aren’t on jobs, then it only has viable people to pick.

Any help would be appreciated.

Hi there,

After having a quick glance at the code I couldn’t quite make sense of what was going on, so I can’t give an answer specific to your code at the moment, however I feel unset() may be of use.
[php]unset($array[$key]);
[/php]
The above would remove the element (person) with a key of $key from the array.

Hi Smokey,

I have tried using unset but the problem is that when it detects that person is in a job the loop needs to loop back to after the array has already been formed. And I’m not sure how to do that.

Function that gets called from the webpage visited.
function setStaging($boardpoint,$task) {

Selects a random person from database and builds array
function getRandomPerson($boardpoint,$task,$n) {

Adds random person to database after doing a check
function addPersonToBackup($boardpoint,$person,$task,$n) {

Checks if person is already doing a job, if it is, loop back to get another random person
function stagingChecker($boardpoint,$person,$task,$n){

^
 l

If it could do that loop back to after the array has already been populated then I could unset the person who is $person (ther person selected)
But I’m not sure how…

Turns out it was an error in another checker.
[php] switch($boardpoint){
case 9: if($data[$i][‘bcmoperator9’] == $person || $data[$i][‘bcmsweeper9’] == $person){
$this->getRandomPerson($boardpoint,$task,$n);
}
[/php]

I forgot to put break;

But it also loops if there aren’t enough people left to put in a job. Is it possible to return false if it runs out of memory?

I don’t know if it’s possible in your code, but if you have an id for people or possibly using their name, each time you check a person’s job you could append them to an array called $used:
[php]$used[] = $person;
[/php]

And at the top of the function you could run:
[php]if(count(array_unique($used) == count($data)) //if the people in the $data array have all been added to the $used array
{
return false;
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service