If the subject is a bit ambiguous my apologizes. I working on my first PHP project and trying to make it Object Oriented. The problem I had was that I was generating a quiz from a database and using the entries in the database as answers to a question. I was using a random function to pull from the database. The problem is that sometimes I would have the same entry numerous times. I created a function to filter double entries and it worked. I wanted to solve the problem without coming here. I have solved it, but I want to see how someone else would have solved it.
[php]
function checkforMatchingAnswers($checkresult){
static $counter;
$counter++;
static $answerholder = array();
$answerholder[$counter] = $checkresult;
$result = array_unique($answerholder);
if(isset($result[$counter])){
echo $result[$counter];
}else{
$option = new Test();
$newanswer = $option->WrongAnswer();
$newanswer;
}
}
}
[/php]
What the method essentially does is when it’s called, it populates an array, then I look for any matches using array_unique method. Then I make sure that there is an actual value inside the variable using isset method. If for what ever reason it is not set, I then call another method which is calls my database to grab another entry. How would anyone do this better?