views:

114

answers:

2

Hello,

I'm trying to get a database query which is an object converted to an associative array, so that I can use it in the calendar class in codeigniter.

This is my model:

<?php

class Get_diary_model extends Model {

    function getAllDiaries($year,$month) {

        $data = $this->db->query("SELECT day AND entry FROM diary WHERE month=$month AND year=$year"); // the entries for the relevant month and year

        foreach($data->result_array() as $row) { // return result as assoc array to use in calendar
            echo $row['day'];
            echo $row['entry'];
        }

        return $data;
        }
    }

and this is the error I get:

atal error: Cannot use object of type CI_DB_mysql_result as array in C:\wamp\www\mm\system\libraries\Calendar.php on line 219

Any ideas?

+1  A: 

Check ou this video tutorial, it will help you -> http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-the-calendar-library/

Your model should look like this:

    function getAllDiaries($year,$month)
    {
        $q = $this->db->query("SELECT day AND entry FROM diary WHERE month=$month AND year=$year");

        if($q->num_rows() > 0):
            foreach($q->result() as $row):
                $data[] = $row;
            endforeach;
            return $data;
        else:
            return false;
        endif;
    }

and your controller:

    function index($year = null, $month = null)
    {
        $this->load->model('Get_diary_model');

        if (!$year) {
            $year = date('Y');
        }
        if (!$month) {
            $month = date('m');
        }

        $data['calendar'] = $this->Get_diary_model->getAllDiaries($year, $month);
}
krike
Perfect! Thank you very much, my controller is similar to that, I just couldn't figure out the model part. Will check out the video as well...
Robimp
you are welcome :)
krike
you can vote up my answer as well :)
krike
i need 15 reputation :-(
Robimp
There you go. It was a good question so i upvoted your question
krike
Excellent, thanks!
Robimp
+2  A: 

The problem was not in your use of result_array(), more that you later return $data directly. $query = $this->db->query() then use $query->result_array() in the foreach. Then you can return $data after building it up in the foreach.

The other answer is a long-winded way of writing the following:

function getAllDiaries($year,$month)
{
    $sql = "SELECT day AND entry FROM diary WHERE month=$month AND year=$year";
    return $this->db->query($sql)->result();
}

But of course that will return an array of objects, not an multidimensional array.

Phil Sturgeon
Hi Phil thanks for the reply, I gave that a try but it doesn't quite do what I want. I'm trying to get the 'day' field from the db to be the array key, and the 'entry' the value. So far, no luck.
Robimp
var_dump() is the answer here. Whenever you get an unexpected result, var_dump(), see what is there and act accordingly. To get an assoc array you are using the correct syntax. It is something in your foreach or array manipulation later that is going wrong.
Phil Sturgeon