php Codeigniter:将数据从控制器传递到视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9446700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter: Passing data from controller to view
提问by Andrew Lynch
I want to pass $data
from the controller named poll
to the results_view
however I am getting an undefined variable error.
我想$data
从名为的控制器传递poll
给results_view
但是我收到一个未定义的变量错误。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Poll extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('form');
}
public function index()
{
$this->load->view('poll_view',$data);
}
public function vote()
{
echo "Voting Successfull";
$this->db->insert('votes',$_POST);
}
public function results()
{
echo "These are the results";
//$query = $this->db->get('votes');
$data = "hello";
$this->load->view('results_view', $data);
}
}
Results_view.php
结果_view.php
<html>
<?php echo $data; ?>
</html>
回答by Lawrence Cherone
$data
should be an array or an object: http://codeigniter.com/user_guide/general/views.html
$data
应该是一个数组或一个对象:http: //codeigniter.com/user_guide/general/views.html
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('results_view', $data);
results_view.php
results_view.php
<html>
<?php
//Access them like so
echo $title.$heading.$message; ?>
</html>
回答by itachi
In simple terms,
简单来说,
$data['a'] in controller becomes $a in your view. ($data won't exist in your view, only the index will become available)
控制器中的 $data['a'] 在您的视图中变为 $a 。(您的视图中将不存在 $data,只有索引可用)
e.g.
例如
Controller:
$data['hello']='hellow world';
view:
echo $hello;
回答by Space
You just need to create a array, you using codeigniter right?
你只需要创建一个数组,你使用 codeigniter 对吗?
Example on controller:
控制器示例:
$data['hello'] = "Hello, world";
$this->load->view('results_view', $data);
In de page "results_view" you just have to:
在页面“results_view”中,您只需:
<?php echo $hello;?>
Obs: You can create n datas, just pay attention in the name and make it a array.
Obs:可以创建n个数据,注意名字,做成数组即可。
Obs2: To use the data use the key of the array with a echo.
Obs2:要使用数据,请使用带有回声的数组的键。
回答by Tom
The view wouldn't call the data 'data'
视图不会将数据称为“数据”
The controller would include an associative index (not sure if that's correct nomenclature) for data e.g 'stuff' looking thus $data['stuff']
控制器将包括数据的关联索引(不确定这是否正确命名),例如“东西”看起来如此 $data['stuff']
You'd echo
in the view so: echo $stuff;
not echo $data;
你会echo
这样认为:echo $stuff;
不是echo $data;
I am a v lowly code fiddler but do really like CodeIgniter so excuse me if i've got this arse about tit.
我是一个卑微的代码提琴手,但我真的很喜欢 CodeIgniter,所以如果我对山雀有这种看法,请原谅我。
One more thing - surely your constructor function is a bit of a waste. All that loading of libraries and helpers is done with the autoload file.
还有一件事 - 你的构造函数肯定有点浪费。所有库和帮助程序的加载都是通过自动加载文件完成的。
回答by Farhad Misirli
You can create property $data = []
; inside CI_Controller(path: system/core/Controller.php) and store all data to show in view. U can load common data like languages, menu, etc in CI_Controller. Also u can add special data for view in controller. (example: $this->data['message'] = "Hello world";
)
Finally, u can pass $this->data
to view when load view (example: $this->load->view('view_name',$this->data);
)
您可以创建属性$data = []
;内部 CI_Controller(path: system/core/Controller.php) 并存储所有数据以显示在视图中。您可以在 CI_Controller 中加载常用数据,如语言、菜单等。您也可以在控制器中为视图添加特殊数据。(例如:$this->data['message'] = "Hello world";
)最后,U可以通过$this->data
查看时负载视图(例如:$this->load->view('view_name',$this->data);
)
I hope this will help you
我希望这能帮到您
回答by noushad mohammed
you can do it this way
你可以这样做
defined array in controller
控制器中定义的数组
$data['hello'] = "hello";
and pass variable to view
并传递变量以查看
echo $hello;
回答by heySushil
If you pass
如果你通过
$data = your code
$this->load->view('your-page', $data);
and get data on your view as
并获取有关您的视图的数据
<?php echo $data;?>
It won't work because ci didn't understand this patern. If like to pass value form controller to view so you can try this -
这是行不通的,因为 ci 不理解这种模式。如果想传递值表单控制器来查看,那么你可以试试这个 -
controller -
控制器 -
$data['any-name'] = your values;
$this->load->view('your-page', $data);
then in your view you can get this data by -
然后在您看来,您可以通过以下方式获取此数据 -
<?php echo $any-name;?>
Hope this helps you.
希望这对你有帮助。
回答by heySushil
In your controller you can pass
在您的控制器中,您可以通过
$data['poll'] = "Your results";
In your view you can call
在您看来,您可以调用
echo $poll;
回答by Reavitor1 Christian in real li
Ok so I finally solved it. You should really have a model (it helps a lot)
好的,所以我终于解决了。你真的应该有一个模型(它很有帮助)
In your model do something like
在你的模型中做类似的事情
Model
模型
class poll_model extends CI_MODEL {
function __construct() {
$this-load->database();
}
function get_poll {
$this->db->query("SELECT * FROM table");
$row = $query->row();
$obj = array(
'id' => $row->id
);
return $obj;
}
}
Now if you have more than an id say name of poll # you can add in array. Now in your controllerdo
现在,如果您有多个 id 说 poll # 的名称,您可以添加到数组中。现在在你的控制器中做
class Poll extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('form');
$this->load->model('poll_model');
}
public function index()
{
$data["a"] = $this->poll_model->get_poll();
$this->load->view('poll_view',$data);
}
And finally in VIEWput
最后在VIEW 中放入
<? echo $a["id"]; ?>
This is a big help. I figured it out by testing and it works for me.
这是一个很大的帮助。我通过测试弄清楚了,它对我有用。
回答by kulandai yesu
In controller:
在控制器中:
$data["result"] = $this->login_model->get_login(); // Get array value from DB..
$this->load->view('login-form',$data); // Pass the array to view
In view:
鉴于:
print_r($result); // print the array in view file