[英]CodeIgniter pass data from model to controller to view
I'm having trouble passing data from the controller to the view using Codeigniter 我在使用Codeigniter将数据从控制器传递到视图时遇到麻烦
My model: 我的模特:
public function get_post($post_id) {
$sql = "SELECT * FROM cities WHERE id = ? AND active ='y' LIMIT 1";
$query = $this->db->query($sql, $post_id);
return $query->result();
}
My controller: 我的控制器:
public function city($post_id) {
$this->load->model('post_model');
$data = $this->post_model->get_post($post_id);
$this->load->view('post_view', $data);
}
My view: 我的观点:
<h3 class="centered"><?php echo $title; ?></h3>
<img src="<?php echo base_url(); ?>uploads/max_<?php echo $photo_name; ?>" alt="Post Photo">
<p><?php echo $city; ?>, <?php echo $state; ?>,
[<?php echo strtoupper($gender); ?>] <?php echo date("D M j g:i a", strtotime($post_date)); ?>
When I do a print_r
in the controller on $data
I get: 当我在
$data
上的控制器中执行print_r
,我得到:
Array ( [0] => stdClass Object ( [id] => 2 [title] => Sample Title [city] => San Francisco [state] => California [gender] => F [photo_name] => 1404181142680616849.jpg [post_date] => 2014-07-01 03:21:41 [active] => y ) )
Change return $query->result();
更改
return $query->result();
in your model to return $query->row();
在您的模型中
return $query->row();
as right now you're getting an array wrapping the actual data you want 现在,您将得到一个包装所需数据的数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.