I have the following function:

function rank() {
        $sql = "SELECT * FROM applicant_details";
        $results = $this->db->query($sql)->result();
        $sql3 = "SELECT * FROM job_advert";
        $job_advert = $this->db->query($sql3)->result();

    foreach ($results as $applicant) {
        $applicant_age = $applicant->age;
        $applicant_id_number = $applicant->id_number;

        foreach ($job_advert as $advert) {
            $job_title = $advert->job_title;
            $age = $advert->age;

            if ($applicant_age >= $age) {
            echo '<pre>';
            echo '</pre>';

            echo '<pre>';
            echo '</pre>';


This function will display the candidates id_number that matches the age requirement of a job advert and the job title it matches. How would I give these candidates a rank, ie. if candidate age matches job_advert age candidate +1, then pull out the best ranking candidates. what would be the best way to do so?
I ave tried inpuing a variable that increment for each matching candidate, bt this failed as it incremented for each record, eg. I have 10 records, then it would display the number of each maching record.
ps. I am still a student and this is for educational purposes(just incase someone thinks "my organization" is discriminatory about age)

4 Years
Discussion Span
Last Post by broj1

In your foreach loop I would put applicants' data in an array and add a rank which is just a difference between an applicant's age and advertised required age (assuming that x years older and x years younger candidates rank equally). Then the array should be sorted.

// this will hold data about the applicants
// ID => rank
$aplicants = array();

foreach ($job_advert as $advert) {
    $job_title = $advert->job_title;
    $age = $advert->age;

    $rank = $applicant_age - $age;

    $aplicants[$applicant_id_number] = $rank;

// sort by values and maintain index information (applicant's ID)

// now you can process the applicants array where the first key(s) have the best ranks

This wont work for what I need.. age is only one of many requirements, sorry, I should have been more specific on that.. The advert requirements also have industry_type, qualification and qualification level, and job_experience(in the spesific field).


Then you have to establish similar criteria for other requirements (i.e. if the qualification equals the required degree then rank it with 0 otherwise 1, 2, 3 or so). The ranks for different requirements have to be somehow comparable. Then you can sum up rank values for each person and the one(s) with the lowest value are the best candidates.


is there not a way to do it with keeping a score of some sort(i.e. if the persons age is a match then $rank = 1 if the person also meets the required qualification $rank +=1, now $rank would be 2).
is tehre a way of doing it like this? sorry if its a noobish question..


@broj1 So Ive done the ranking in reverse as you suggested, but now how do I get say a top 10 of those that ranked the best?

P.S. I am using asort($applicants); as you have suggested.


The $applicants array now contains applicants IDs as keys and ranks as values, sorted by lowest value first (lowest value = highest rank). You can also use a foreach loop:

// define a counter so you can see when you have 10 applicants
$counter = 1; 

// display an introductory text
echo 'The 10 most appropriate applicants are:<br>';

// loop through the $applicants array
foreach($applicants as $id => $rank) {

    // display each applicant's ID and rank
    // (in reality you would get and display it's name)
    echo "Sid, ranked $rank<br>";

    // check if the counter reached 10 to break out of the loop
    if($counter == 10) {

    // increase the counter

This is just a principle you can adapt to your needs.

Edited by broj1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.