简体   繁体   中英

how to use a query result() value in another query[codeigniter]

Update

All of your methods work. Thanks @NiloySaha, @AeroX, @Ray, @SusheelMishra.

But what if I want to compare more fields such as work experience, salary, etc? @Ray do I just add more params like so

function check_job_adverts($age, $experience, $salary){

}

?

or how would I approach this? I am relevantly new to this, so please excuse me if I ask noobish questions..


I have the following:

function age() {
        $sql = "SELECT * FROM membership";
        $query = $this->db->query($sql)->result();
        foreach ($query as $row) {
            $id = $row->id_number;
            $dobs = substr($id, 0, 6);
            $dob = str_split($dobs, 2);
            $day = date('d', mktime(0, 0, 0, 0, $dob[2], 0));
            $month = date('m', mktime(0, 0, 0, $dob[1] + 1, 0, 0));
            $year = date('o', mktime(0, 0, 0, 0, 0, $dob[0] + 1));
            $date = "$day/$month/$year";
            //explode the date to get month, day and year
            $date = explode("/", $date);
            //get age from date or birthdate
            $age = (date("md", date("U", mktime(0, 0, 0, $date[0], $date[1], $date[2]))) > date("md") ? ((date("Y") - $date[2]) - 1) : (date("Y") - $date[2]));
            return $age;
        }

I want to know how to use the $age variable in another query. for example: I want to compare $age with a job_advert table which can have an age requirement, and then display all in membership table that meets the required age from job_advert .

If i am guessing right you need to compare years. So use column instead of m, d, y

select FLOOR(DATEDIFF('2013-07-19','2013-05-10'))

You can use mysql to calculate the age of the person rather then doing it in PHP thus eliminating the need to run multiple queries.

SELECT YEAR( DATEDIFF( CURDATE(), DATE(CONCAT( LEFT(id_number,2), '-', MID(id_number,3,2), '-', MID(id_number,5,2) ))) ) AS AGE

Something like the above should work to convert the first 6 chars of the id_number into a date and compare it to the current date and get the difference in years.

Try :

function age() {
    $data   = array();
    $sql = "SELECT * FROM membership";
    $query = $this->db->query($sql)->result();
    foreach ($query as $key=>$row) {
        $id = $row->id_number;
        $dobs = substr($id, 0, 6);
        $dob = str_split($dobs, 2);
        $day = date('d', mktime(0, 0, 0, 0, $dob[2], 0));
        $month = date('m', mktime(0, 0, 0, $dob[1] + 1, 0, 0));
        $year = date('o', mktime(0, 0, 0, 0, 0, $dob[0] + 1));
        $date = "$day/$month/$year";
        //explode the date to get month, day and year
        $date = explode("/", $date);
        //get age from date or birthdate
        $age = (date("md", date("U", mktime(0, 0, 0, $date[0], $date[1], $date[2]))) > date("md") ? ((date("Y") - $date[2]) - 1) : (date("Y") - $date[2]));
        #return $age;                       #not returning the age now instead inserting in a array
        $data[$key]['id'] = $id;
        $data[$key]['age'] = $age;
    }
    $data = $this->another_funcion_call($data);     #calling another function which will recieve the array of ids / age
    return $data;
}

function another_funcion_call($arrayOfIdsAge){
    //loop through the array and do something
    foreach($arrayOfIdsAge as $key=>$each){
        echo "ID :".$each['id'].' AGE : '.$each['age'].'<br>';
    }
}

Just an example if you dont want to call it in the same function as age();

function age() {
    $sql = "SELECT * FROM membership";
    $query = $this->db->query($sql)->result();
    foreach ($query as $row) {
        $id = $row->id_number;
        $dobs = substr($id, 0, 6);
        $dob = str_split($dobs, 2);
        $day = date('d', mktime(0, 0, 0, 0, $dob[2], 0));
        $month = date('m', mktime(0, 0, 0, $dob[1] + 1, 0, 0));
        $year = date('o', mktime(0, 0, 0, 0, 0, $dob[0] + 1));
        $date = "$day/$month/$year";
        //explode the date to get month, day and year
        $date = explode("/", $date);
        //get age from date or birthdate
        $age = (date("md", date("U", mktime(0, 0, 0, $date[0], $date[1], $date[2]))) >     date("md") ? ((date("Y") - $date[2]) - 1) : (date("Y") - $date[2]));
        return $age;
    }

function check_job_adverts($age){
// here goes your job_advert checking function
// example.. run an array to match ages vs jobs
// mind the array return if multiple options
}




$age = age();
// somewhere else in your script make the call with $age
$fetchJobs = check_job_adverts($age);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM