2

我正在使用 CodeIgniter 2.1.0 和 MySQL。我想将水平数据行显示为垂直数据行。当我从数据库中获取一行并回显它时,它看起来像

----------------------------------------
id    | name   | address | email       |
----------------------------------------
1     | Foo    | Bar     | foo@bar.com |
----------------------------------------

我已经使用 CodeIgniters 表库来生成上表。而不是这个,我希望它像这样显示:

------
id : 1
name: foo
address : bar
email: foo@bar.com
-------------------

如何使用 CodeIgniter 2.1.0 做到这一点?

4

1 回答 1

3

如果您使用的是模板视图,那么这是更好的程序示例代码:视图->模板:

<?php $this->load->view('includes/header');?>
<?php $this->load->view($main_content);?>
<?php $this->load->view('includes/footer');?>

模型:

function detail()
{
 $this->db->where('id',$this -> session -> userdata('id'));
 $query=$this->db->get('user');
 $row=$query->row_array();
 return $row;
}

控制器:

$this->load->model('my_model');
$this->my_model->detail();
$data=array(
 'id'=>$query['id'],
 'name'=>$query['name'],
 'address'=>$query['address'],
 'email'=>$query['email']
);
$data['main_content'] = 'your_view';
$this->load->view('my_view',$data);

看法:

<div>
id : <?php echo $id;?><br/>
name: <?php echo $name;?><br/>
address: <?php echo $address;?><br/>
email: <?php echo $email;?>
</div>

在代码视图中使用模板始终是首选和假定的良好做法。

于 2012-01-28T13:28:48.973 回答