This function always returns FALSE:

function rank($applicant_id_number = 0) {
        $this->load->model('advert_model');
        $application = $this->advert_model->get_applications();
        // $user = $this->flexi_auth->get_user_by_identity_row_array();
        $rank = 0;

        $sql1 = "SELECT * FROM applicant_details";
        $results = $this->db->query($sql1)->result();
        $sql3 = "SELECT * FROM job_advert";
        $job_advert = $this->db->query($sql3)->result();
        foreach ($results as $key => $applicant) {
            $applicant_age = $applicant->age;
            $applicant_id_number = $applicant->id_number;
            $applicant_job_experience = $applicant->job_experience;
            $applicant_industry_type = $applicant->industry_type;
            $applicant_qualification_type = $applicant->qualification_type;
            $applicant_city = $applicant->city;
            $applicant_relocate = $applicant->relocate;
            $applicant_first_name = $applicant->firstname;
            $applicant_phone = $applicant->upro_phone;
            $applicant_min_salary = $applicant->minimum_salary;
            $applicant_prefered_salary = $applicant->preferred_salary;
            $applicant_licence = $applicant->license;
            $applicant_employment_equity = $applicant->employment_equity;
            $applicant_email = $applicant->uacc_email;
            $applicant_gender = $applicant->gender;
            $applicants = array();
            $sql = "SELECT DISTINCT account.uacc_group_fk, demo.id_number 
FROM user_accounts AS account
INNER JOIN demo_user_profiles AS demo ON account.uacc_id = demo.upro_uacc_fk
WHERE demo.id_number = {$applicant_id_number}";
            $user_group = $this->db->query($sql)->result();
            foreach ($user_group as $group) {
                $admin = $group->uacc_group_fk;
                // print_r($admin);
                foreach ($job_advert as $key => $advert) {
                    $advert_id = $advert->advert_id;
                    $job_title = $advert->job_title;
                    $salary_offered = $advert->salary_offered;
                    $is_negotiable = $advert->negotiable;
                    $company_location = $advert->company_location;
                    $Job_experience = $advert->required_experience;
                    $age = $advert->age_group;
                    $male = $advert->male;
                    $female = $advert->female;
                    $job_industry = $advert->industry_type;
                    $employment_equity = $advert->employment_equity;
                    $qualification_type = $advert->qualification_type;
                    $qualification_name = $advert->qualification_name;
                    $drivers_licence = $advert->drivers_licence;
                    $status = $advert->status;


                    //check age group
                    if ($age = "b") {
                        $agegroup = "18-23";
                    } else if ($age = "c") {
                        $agegroup = "24-30";
                    } else if ($age = "d") {
                        $agegroup = "31-40";
                    } else if ($age = "e") {
                        $agegroup = "40-1000";
                    }
                    $agegroups = explode("-", $agegroup);
                    // Rank age group
                    if ($age === "any" || $applicant_age >= $agegroups[0] || $applicant_age <= $agegroups[1]) {
                        $applicants[$applicant_id_number] = $rank;
                    } else {
                        $applicants[$applicant_id_number] = $rank + 5;
                    }
                    // Rank industry type && job_experience
                    if ($applicant_industry_type == $job_industry || $applicant_industry_type != NULL) {
                        $applicants[$applicant_id_number]+=0;
                        if ($applicant_job_experience >= $Job_experience || $applicant_job_experience != NULL) {
                            $applicants[$applicant_id_number]+=0;
                        } else {
                            $applicants[$applicant_id_number]+=30;
                        }
                    } else {
                        $applicants[$applicant_id_number]+=30;
                    }
                    // rank salary
                    $p_salary = explode("-", $applicant_prefered_salary);
                    $m_salary = explode("-", $applicant_min_salary);

                    if ($p_salary[0] >= $salary_offered && $p_salary[1] <= $salary_offered || $m_salary[0] >= $salary_offered && $m_salary[1] <= $salary_offered || $is_negotiable) {
                        $applicants[$applicant_id_number] +=0;
                    } else {
                        $applicants[$applicant_id_number] = $rank + 20;
                    }

                    //rank qualification Type
                    if ($applicant_qualification_type == $qualification_type || $qualification_type != NULL || $applicant_qualification_type != NULL) {
                        $applicants[$applicant_id_number]+=0;
                    } else {
                        $applicants[$applicant_id_number]+=30;
                    }
                    //rank by gender
                    if ($applicant_gender < 5000) {
                        $gender = "female";
                    } else {
                        $gender = "male";
                    }

                    if ($male == 1 && $gender = "male") {
                        $applicants[$applicant_id_number]+=0;
                    } else if ($female == 1 && $gender = "female") {
                        $applicants[$applicant_id_number]+=0;
                    } else if ($male == 0 && $female == 0) {
                        $applicants[$applicant_id_number]+=0;
                    } else {
                        $applicants[$applicant_id_number]+=35;
                    }
                    //check if user group this is not to rank admin users
                    if ($admin == 1) {
                        $applicants[$applicant_id_number]+=0;
                    } else {
                        $applicants[$applicant_id_number]+=90;
                    }
                    //rank employment_equity
                    if ($applicant_employment_equity == $employment_equity || $employment_equity == "any") {
                        $applicants[$applicant_id_number]+=0;
                    } else {
                        $applicants[$applicant_id_number]+=25;
                    }

                    //rank drivers licence
                    if ($applicant_licence == $drivers_licence || $applicant_licence != NULL && $drivers_licence != NULL || $applicant_licence == NULL && $drivers_licence == NULL) {
                        $applicants[$applicant_id_number]+=0;
                        if ($applicant_licence != NULL && $drivers_licence = NULL) {
                            $applicants[$applicant_id_number]+=0;
                        }
                    } else {
                        $applicants[$applicant_id_number]+=5;
                    }
                    if ($applicant_industry_type == $job_industry) {
                        $applicants[$applicant_id_number]+=0;
                    } else {
                        $applicants[$applicant_id_number]+=30;
                    }

                    asort($applicants);

                    $counter = 1;
                    $id_number = array();
                    $advert_identity = array();
                    $id_number['id_number'] = $applicant_id_number;
                    $advert_identity['advert_id'] = $advert_id;

                    if ($applicants[$applicant_id_number] < 35) {
                        $this->called = TRUE;
                        $this->set_rank_results($applicant_id_number, $advert_id, $applicants[$applicant_id_number], $this->applied);
                        $sql = "DELETE FROM rank_results 
                            WHERE job_id in (SELECT advert_id FROM job_advert WHERE status =0)";
                        $this->db->query($sql);
                        return TRUE;
                    } else {
                        return FALSE;
                    }


                }
            }

        }
    }

Why wont it return TRUE? and how can I make it return TRUE if a applicants rank is less than 35?

Well, this function looks rather complex. This is valid, but quite hard to debug. But, let's try it anyway. Put this temporary debug code on line 149:

echo $applicants[$applicant_id_number] . '<br>';

This should presumably display somewhere all the values of the $applicants array used in the if test (if not, write a test script). You can inspect the values and try to understand why they do not get over 35. Please post the output here.

it just dispays 45. which I cant understand why... It is supposed to list many results... I am not quite understanding this.. I removed the return True/False and then it works... any ideas on why it could be doing this?

I think, It is because When I return, it stops the function, so it wont go through the foreach loop any further... and that is hy it only diplays 45 when I echo out $applicants[$applicant_id_number].

This just caught my eye
line 105
if ($male == 1 && $gender = "male")
shoulden that be :
if ($male == 1 && $gender == "male")
also on line 107

commented: good catch +6
commented: Indeed +9

How do you call this function? And what is the purpose? As far as I understand it if applicant's rank is below 35 he/she gets deleted from the table. But how do you test for the return value (which is TRUE in case of deletion)? And why do you pass a certain applicant's ID, then query for applicants (I am guessing) and then loop through results?

And I just noticed it: in the if test on line 150 you return in any case (true or false) so the foreach loop gets thru only one time and is therefore not needed really. Maybe your return statements are in wrong places?

BTW: do not forget to correct lines 105 and 107 as pzuurveen suggested above?

Thank you @pzuurveen for pointing that out...
it only deletes the records if the advert status is 0, I should have included the function:set_rank_results($applicant_id_number, $advert_id, $applicants[$applicant_id_number], $this->applied) on line 152, which writes the records to a table:

function set_rank_results($applicant_id_number, $advert_id, $applicants = array(), $applied) {
    $sql = "INSERT INTO rank_results (applicant_id,job_id,rank, applied)
VALUES('{$applicant_id_number}','{$advert_id}','{$applicants}','{$applied}')
                    ON DUPLICATE KEY UPDATE
                   rank = '{$applicants}',
                   applied = '{$applied}'";
    $this->db->query($sql);

}

@broj1 I use this funtion twice.
1) I use it to to rank the candidates, and then display results in an html table.
2) I want to use it when candidates apply for a job to make sure they qualify for the job they're applying to. I use the following funtions:
in my model underneath function rank{ ... } I have this:

function apply_rank($id_number) {
    $this->applied = 1;

    if ($this->rank($id_number) && $this->called === TRUE) {            
        return TRUE;
    } else {
        return FALSE;
    }
}

In my controller:

function apply($advert_id) {
//get consultant name
    $sq = $this->db->select('consultant_name')->from('job_advert')->where('advert_id', $advert_id)->get();

    $data['id_number'] = $this->session->userdata('id_number');
    $data['advert_id'] = $advert_id;
    $data['status'] = 'pending';
    foreach ($sq->result() as $consultant) {
        if (!$consultant->consultant_name == "") {
            $data['consultant'] = $consultant->consultant_name;
        } else {
            $data['consultant'] = "not yet assigned";
        }
    }  

 #This is where I check if applicant qualifies for job.

    if ($this->ranking_model->apply_rank($data['id_number']) === TRUE) {
        if ($this->Advert_model->apply($data) === TRUE) {
        // redirects to view applicants application
            redirect("advert/applications/" . $data['id_number']."");
        } else {
        // redirects to view applicants application
            echo "<script>alert('You have already applied for this job.');window.location.href = '" . base_url() . "index.php/advert/applications/" . $data['id_number'] . "';</script> </a>";
        }
        $this->Advert_model->update_applicants($advert_id);
    }else{
    // redirects to view applicants application
        echo "<script>alert('You do not meet minimum requiremens for this job, try to improve your profile by providing accurate information.');window.location.href = '" . base_url() . "index.php/advert/applications/" . $data['id_number'] . "';</script> </a>";
    }
}

the passing of the applicants ID was just a test to see if I can only get one result.

somthing that seems so simple turn out to be the things that irretate you the most...

I have thought that maybe they are in the wrong places, but I do not know where to put them if they are in the wrong place

if you split this big function to a small separate function it will be simple to detecte and correct errors

try to change and develop your manner in coding. because in this way you can't resist in big projects.

Best regards

I have thought that maybe they are in the wrong places, but I do not know where to put them if they are in the wrong place

That depends on the logic (what you want to achieve). But at least one return statement is in wrong place. i.e if the code on line 159 would be moved to the place just after the line 166 the foreach loop could go through more iterations. But I do not know if this is what you want to achieve.

if you split this big function to a small separate function it will be simple to detecte and correct errors

I think 203428's suggestion is excelent. Your function is doing a lot of small tasks that can be split into smaller functions. That would make the code much more readable and easier to debug. For example code between lines 55 and 65:

//check age group
if ($age = "b") {
    $agegroup = "18-23";
} else if ($age = "c") {
    $agegroup = "24-30";
} else if ($age = "d") {
    $agegroup = "31-40";
} else if ($age = "e") {
    $agegroup = "40-1000";
}
$agegroups = explode("-", $agegroup);

could be made into a function (or method of a class):

function getAgeGroupArray($age) {

    if ($age = "b") {
        $agegroup = "18-23";
    } else if ($age = "c") {
        $agegroup = "24-30";
    } else if ($age = "d") {
        $agegroup = "31-40";
    } else if ($age = "e") {
        $agegroup = "40-1000";
    }
    return explode("-", $agegroup);    
}

or more concise form (you can stick to the above form if you wish :-):

function getAgeGroupArray($age) {

    switch($age) {
        case 'b': return array('18', '23'); break;
        case 'c': return array('24', '30'); break;
        case 'd': return array('31', '40'); break;
        case 'e': return array('41', '1000'); break;
    }    
}

Now just call the function in the code:

$agegroups = getAgeGroupArray($age);

You can do this with other tasks like deleting a record etc.

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.