视图是用户用户能看到你的网站的所有。 他们使用一个统一的接口, 而且可以根据需要进行修改。 mvc 的好处之一是你分开了表示层和逻辑层, 一切都显得很干净。视图实际上是一组包含有你的内容的html结构。结构中有各种元素,如颜色,字体,文字布局等; 不过视图不关心这些,它要做的只是取来内容,显示出来。
一般我们在控制器中这样定义:
function index() { $data['mytitle'] = a website monitoring tool; $data['mytext'] = this website helps you to keep track of the other websites you control.; $this->load->view('basic_view',$data);}
我们把$data数组作为$this->load->view()的第二个叁数,在视图名称之后。视图接收到$data数组后,使用php函数extract()把数组中的每个元素转换成内存变量,数组的键名即为变量名,值为变量内所包含的值。这些变量的值能直接被视图引用:
虽然你只能传送一个变量到视图, 但是通过建立数组,你能把大量变量整洁地传入视图。它似乎复杂, 但是实际上是一种紧凑和优秀的信息传输方式。
如果传递过来的数组是包含多个数据,那么就需要遍历,操作一般如下。先是控制器:
$data[notice] =array('aaa','bbb'); $this->load->view('api/notice',$data);
视图中解析:
0){ foreach($notice as $key =>$value ){?>
再说下二维数组的传递与遍历问题。下面的程序实现遍历某个目录下的文件。
load->helper('url'); $data['page_title'] = '帮客之家'; $data['pre_url'] = 'application/views/default/'; $this->load->view('default/header', $data); $this->load->view('default/index', $data); $this->load->view('default/footer'); //$this->load->view('index'); } public function gdnews() { $this->load->helper('url'); $arr = array(); function tree($directory) { $mydir = dir($directory); while($file = $mydir->read()) { // 是目录的话 if((is_dir($directory/$file)) and ($file!=.) and ($file!=..)) { //echo $file\n; //tree($directory/$file); } // 不是目录 else { $arr['name'][] = @iconv('gb2312','utf-8',$file); $arr['time'][] = @iconv('gb2312','utf-8',date('y-m-d h:i:s', filemtime($file))); } } $mydir->close(); return $arr; } $dir = datas/gdnews/; $arr = tree($dir); $data['files'] = $arr; $data['page_title'] = '帮客之家' ; $data['dir'] = $dir; $this->load->view('default/header', $data); $this->load->view('default/data', $data); $this->load->view('default/footer'); }}?>
在视图中这么输出就可以:
0) { foreach($files['name'] as $key =>$value ){?> 查看 删除
http://www.bkjia.com/phpjc/752565.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/752565.htmltecharticle视图是用户用户能看到你的网站的所有。 他们使用一个统一的接口, 而且可以根据需要进行修改。 mvc 的好处之一是你分开了表示层和逻辑...