CodeIgniter传递数据到视图 [英] CodeIgniter passing data to the view

查看:89
本文介绍了CodeIgniter传递数据到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的控制器传递到这样的视图...

  public function playerslist $ b {
$ this-> load-> database();
$ data = $ this-> db-> get('skaters');
$ this-> load-> helper('url');
$ this-> load-> view('playerslist',$ data);
}

在我看来...

 <?php echo $ data; > 

但我收到此错误...
$ b


遇到PHP错误



严重性:通知


$ b b

讯息:未定义的变数:data



档案名称:views / playerslist.php



< 76


我做错了什么?



这些数据放在一个foreach语句中并显示$ data数组中的所有内容

  foreach($ data as $ value = > $ key){
echo $ key。 < br />;
}

谢谢
J

解决方案

您不能直接从视图中访问 $ data 。传递给视图的 $ data 必须是关联数组。



例如:

 <$> c $ c> $ data = array(
'name'=>'John',
'bars'=> 23
);

$ this-> load-> view('playerslist',$ data);

然后,在您的视图中,这些将被转换为变量:

 <?php echo $ name; > has<?php echo $ bars; >巧克力。 






如果要访问其原始数据格式,将 传递到关联数组中:

  $ data = $ this-& > get('skaters') - > result(); 
$ this-> load-> view('playerslist',array('data'=> $ data));


I am trying to pass from my controller to the view like so...

public function playerslist()
    {
        $this->load->database();
        $data = $this->db->get('skaters');
        $this->load->helper('url');
        $this->load->view('playerslist', $data);
    }

and in my view...

<?php echo $data; ?>

but I get this error...

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/playerslist.php

Line Number: 76

What am I doing wrong?

What I would like to do with this data is put in a foreach statement and display everything in the $data array

foreach($data as $value => $key){
echo $key . "<br/>";
}

Thanks, J

解决方案

You cannot access $data directly from your view. The $data you pass to your view has to be an associative array. The keys will then be converted to variables in your view.

For example:

$data = array(
    'name' => 'John',
    'bars' => 23
);

$this->load->view('playerslist', $data);

Then, in your view, those will be converted to variables:

<?php echo $name; ?> has <?php echo $bars; ?> bars of chocolate.


If you want to access the data in its original format, pass that into the associative array:

$data = $this->db->get('skaters')->result();
$this->load->view( 'playerslist', array('data' => $data) );

这篇关于CodeIgniter传递数据到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆