I'm having issues adding a "sub result" to a result in Codeigniter. Not sure how to add to this object.
$result->{$record_id}->threads = $threads;
should equal something like this
$result->1->threads = $threads;
but I can't get it to work... I'm not new to OOP but this is the first I've tried to do this.
<?php
function get() {
$this->db->select(array(
'record_id', 'record_data', 'record_date',
));
$this->db->from('records');
$sql = $this->db->get();
$records = $sql->result();
foreach($records as $record){
$record_id = $record->record_id;
$this->db->select(array(
'thread_id', 'thread_parent', 'thread_data', 'thread_date',
));
$this->db->from('records_thread');
$this->db->where(array(
'thread_recordid' => $record_id,
));
$sql = $this->db->get();
$threads = $sql->result();
# this 开发者_开发问答is where i'm having issues \/
$records->{$record_id}->threads = $threads;
}
return $records;
}
?>
I don't want to use arrays and it's easier to use this data on the view file.
I think you just need:
$record->threads = $threads;
EDIT:
You just need to assign the reference in your foreach (note the &
next to $record
):
foreach($records as &$record)
{
//...
$record->threads = $threads;
}
return $records;
If the method shown by rojoca doesn't work, you could do this:
$records = $sql->result_array();
Then loop through as usual (except do $record_id = $record['record_id']
instead of $record->record_id
), and reference your row like this:
$records[$record_id]['threads'] = $threads;
Though you may not want to rely so heavily on that record_id column always matching perfectly with your iteration index.
精彩评论