0

I want to make a query that results like this in codeigniter MODEL:

$caldata = array (
   15 => 'yes',
   17 => 'no'
);

Is that possible to do?

Take NOTE: The 15,17 and yes,no are in the same database table.

3 Answers 3

1

There is no core helper function to achieve what you want in CI. But you can create your own helper function:

function pluck($arr = [], $val = '', $key = '') 
{
    // val - label for value in array
    // key - label for key in array
    $result = [];
    foreach ($arr as $value) {
        if(!empty($key)){
            $result[$value[$key]] = $value[$val];    
        }else{
            $result[] = $value[$val];    
        }
    }
    return $result;
}
Sign up to request clarification or add additional context in comments.

Comments

1

you can use result_array() function so you can have something like:

$query = $this->db->select('id,answer')->from('users')->get();

$result = $query->result_array();

print_r($result);

After that you have your array and you can make the $key => $value relation of the fields

Comments

0

After a long search i found an answer. Sample way to do this:

$query = $this->db->select('start_date, class')->from('event')->like('start_date', "$year-$month", 'after')->get();

$datavariable = $query->result();

   $caldata = array();
                foreach($datavariable as $row){
                    $caldata[substr($row->start_date,8,2)] = $row->class;
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.