2

original I thought the $query is array object, since I can print these data to a list table like this. my controller code

$data['dates'] = $this->calendar_model->get_cal_eventDate();
$this->load->library('table');
$this->load->view('my_test', $data);

my view code

echo $this->table->generate($dates);

but when I changed my code to try to print $tbDataArr via foreach. It didn't work. How can I convert the $query result (eventDate field values) to array object.

function get_cal_eventDate() {
    $this->db->select('eventDate');
        $query = $this->db->get('tb_event_calendar');
        return $query;
}

$tbDataArr = $this->calendar_model->get_cal_eventDate();

foreach ($tbDataArr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}
2
  • 2
    var_dump($tbDataArr) - what is your output ? Commented Apr 4, 2011 at 12:35
  • What is the current format of result returned? Commented Apr 4, 2011 at 12:35

3 Answers 3

9

Are you using CodeIgniter? If you are you can do this

function get_cal_eventDate() {
    $this->db->select('eventDate');
    $query = $this->db->get('tb_event_calendar');
    $result = $query->result_array();
    return $result;
}

If not need more info about what your doing with your PHP.

Sign up to request clarification or add additional context in comments.

Comments

4

Codeigniter, right?

Use the $query->result_array() method.

see more here:
http://codeigniter.com/user_guide/database/results.html

Comments

0
    //Convert $query->result_array() to $query->result() after add new field in array

    $result = $query->result_array();
    for($i=0;$i<$query->num_rows();$i++){
        //insert new field in array (test_field with value 1)
        $result[$i]+=array('test_field'=>1);
        $result[$i] = (object)$result[$i];
    }
    return $result; //equivalent to return $query->result() without new field;

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.