CodeIgniter将数据从模型传递到控制器以进行查看

时间:2014-07-01 23:28:42

标签: php codeigniter

我在使用Codeigniter

将数据从控制器传递到视图时遇到问题

我的模特:

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();
}

我的控制器:

public function city($post_id) {
  $this->load->model('post_model');
  $data = $this->post_model->get_post($post_id);
  $this->load->view('post_view', $data);
}

我的观点:

<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)); ?>

当我在print_r的控制器中执行$data时,我得到:

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 ) )

1 个答案:

答案 0 :(得分:0)

将模型中的return $query->result();更改为return $query->row();,因为现在您正在获取包含所需实际数据的数组