Thursday, January 1, 2009

How To "display related table field value on View" - first 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.

first way:
you can see post controller have " $sysuser_options " assigned to view.
please 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 $sysuser_options[$m['sysuser_id']]; ?><td>
</tr>

than change the controller show function content.
from

// 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;

to

// 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['name'];
}
$data['sysuser_options']=$sysuser_option;


that's all.

No comments:

Post a Comment