用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3019|回复: 4
收起左侧

[讨论/交流] 新手问题,控制器怎样将参数传递给视图呢?

[复制链接]
发表于 2013-2-4 15:54:05 | 显示全部楼层 |阅读模式
用模型读取数据库信息,最终是要传给视图的,可是怎样传递给视图呢?
我的程序
控制器
___________________________________________
  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Zixun extends CI_Controller {
  public function __construct()
  {
   parent::__construct();
   
   }
public function index()
{
$this->load->model('content');    //这里是想从模型获取数据
      $query=$this->content->get_content(1);
        $this->load->view('zixun',$query ); //这里我想将从模型获得是数据传递给视图
      
}



模型
——————————————————————
<?php
class Content extends CI_Model
{
  
function __construct()
{
  parent::__construct();
  $this->load->database();
}
public function get_content($testid)
{     $this->db->where('id',$testid); //查询数据库
   $this->db->select('*');
   $query=$this->db->get('dili_u_m_content');
   return $query->result();
     }
}
?>




视图
——————————————————————————
<?php
  $this->load->view("header");
$this->load->view("content");
?>
  <?php
$this->load->view("footer");
?>
 楼主| 发表于 2013-2-4 15:55:12 | 显示全部楼层
这个程序视图没显示的,请教一下怎样将数据传递给视图,并且在指定的DIV标签内显示呢?
发表于 2013-2-4 17:13:13 | 显示全部楼层
类似于这样“$data['title'] = 'Your title';
$data['message'] = 'Your message'     
$this->load->view('header',$data);”在视图里直接输出$title就是“Your title”
发表于 2013-2-4 21:56:18 | 显示全部楼层
以下给你一个例子参考
控制器:

  1. public function index()
  2. {
  3.   $this->load->model('content');
  4.   $query['data']=$this->content->get_content(1);
  5.   $this->load->view('zixun',$query);
  6. }
复制代码
视图

  1. <?php
  2. print_r($data) // 检查array中是否有资料

  3. foreach($data as $d){
  4.   echo $d->'column' ; // 'column' 改成资料库对应的栏位名称
  5. }
  6. ?>
复制代码
 楼主| 发表于 2013-2-5 01:11:36 | 显示全部楼层
谢谢大家

本版积分规则