Thursday, January 1, 2009

How To "display related table field value on View" - second way

refer to "step by step" post.
post table's "sysuser_id" field is a foreign key, which related to sysuser table.

this post show that how to show sysuser table's name field value on post's show record page.

you have 2 way for get sysuser's name.
1. use sysuser options array.
2. left join table when get the record.

second way:
edit the post controller's show function
remove all code which for create sysuser options array.

// sysuser options array
$this->sysuser_id_options=array();
$this->load->model('sysuser_model');
$records=$this->sysuser_model->get_records();
$sysuser_option=array(''=>'');
foreach($records as $record){
$sysuser_option[$record['id']]=$record['id'];
}
$data['sysuser_options']=$sysuser_option;



then change show function's code
form

$data['m']=$this->post_model->get_record_by_pk($m_id);

to

$this->post_model->set_related_table_field('sysuser','sysuser.name as uname');
$this->post_model->add_where(array('post.id'=>$m_id));
$data['m']=$this->post_model->get_record();


change post's view show.php file.
from

<tr>
<th><?php echo form_label('Sysuser Id:', 'sysuser_id'); ?><th>
<td><?php echo $m['sysuser_id']; ?><td>
</tr>

to

<tr>
<th><?php echo form_label('Sysuser Id:', 'sysuser_id'); ?><th>
<td><?php echo $m['uname']; ?><td>
</tr>


that's all

No comments:

Post a Comment