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>';
            var_dump($applicant_id_number);
            echo '</pre>';

            echo '<pre>';
            var_dump($job_title);
            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)

Dani AI

Generated

was asking how to rank candidates when multiple fields matter. and pointed toward a score-based approach; the missing pieces are (1) how to convert each requirement into a comparable numeric score, (2) how to weight those scores, and (3) how to get a top‑N result efficiently. The approach below keeps the simple “score += 1 for a match” idea but generalizes it so different attributes (age closeness, qualification level, industry match, years of experience) can be combined and tuned.

Convert each attribute to a 0..X contribution, then sum with weights. Examples:

  • Categorical exact match (industry, certification): binary 0 or 1 times a weight.
  • Qualification level: map strings to numbers (none=0, diploma=1, bachelors=2, masters=3, phd=4) and give a positive score when applicant level >= required level.
  • Experience: normalize years (e.g., min(job_required, applicant_years) / job_required) times a weight.
  • Age: use closeness rather than exact equality, e.g. age_score = max(0, 1 - abs(app_age - req_age)/D) where D controls sensitivity.

For medium/large datasets compute the score in SQL and ask the DB for the top matches (fast, avoids pulling all rows). Example (adapt column names to the schema):

SELECT a.id_number,
 (CASE WHEN a.industry_type = j.industry_type THEN 30 ELSE 0 END)
+(CASE WHEN a.qualification = j.qualification THEN 25 ELSE 0 END)
+(CASE WHEN a.qual_level >= j.qual_level THEN 20 ELSE 0 END)
+LEAST(10, GREATEST(0, 10 - ABS(a.age - j.age)))
+LEAST(15, GREATEST(0, a.experience_years - j.required_experience)) AS score
FROM applicant_details a
CROSS JOIN job_advert j
WHERE j.id = :job_id
ORDER BY score DESC
LIMIT 10;

For small sets do it in PHP: map levels to numbers, compute each attribute score, sum weighted parts, then sort by the total and slice the first 10 (use usort + array_slice). Store weights in config or the job_advert row so they can be tuned without code changes. Treat missing data conservatively (score 0 or an imputed default). Use secondary tie‑breakers (experience, then age closeness) and be mindful of legal/ethical issues (age is a protected attribute in many jurisdictions) — for production systems consult HR/legal and log how weights are chosen.

Recommended Answers

All 8 Replies

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)
asort($aplicants);

// 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..

What broj1 suggested is keeping score (but in reverse).

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.

Just use a for or while loop to get the first 10 items.

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) {
        break;
    }

    // increase the counter
    $counter++;
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.