I want to pass $data
from the controller named poll
to the results_view
however I am getting an undefined variable error.
我想将$数据从名为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
Results_view.php
<html>
<?php echo $data; ?>
</html>
10 个解决方案
#1
41
$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>
#2
12
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;
#3
6
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']
控制器将包含一个关联索引(不确定是否正确命名)用于数据e。g '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所以如果我有这个关于tit的arse。
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.
还有一件事——您的构造函数肯定有点浪费。所有这些库和助手的加载都是用autoload文件完成的。
#4
4
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个数据,只需注意名称并将其作为数组。
Obs²: To use the data use the key of the array with a echo.
奥林匹克广播服务公司²:使用数据使用数组的键与呼应。
#5
1
In your controller you can pass
在您的控制器中,您可以传递
$data['poll'] = "Your results";
In your view you can call
在你的视野里,你可以打电话
echo $poll;
#6
1
you can do it like this way $data['hello'] = "hello";
您可以这样做,$data['hello'] = "hello";
in view echo $hello;
鉴于echo $你好;
#7
1
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 controller do
现在,如果您有多个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 VIEW put
最后在视图中
<? echo $a["id"]; ?>
This is a big help. I figured it out by testing and it works for me.
这是一个很大的帮助。我通过测试找到了答案,它对我很有用。
#8
0
I have seen all above answer so here is what I do when I have to load the data from the controller to my view. To Pass the Data To the view from the controller:
我已经看到了上面的答案,这是我在从控制器加载数据到视图时所做的。将数据从控制器传递给视图:
public function your_controller(){
// Your Necessary Code
// You have the $data, $data2, $data3 to post to the view.
$this->load->view('your_view_directory or view_page',['data'=>$data, 'data2'=>$data2, 'data3'=>$data3... so on ]);
}
And At the View Side You can simply retrieve that data: To Display You can simply use echo
, print
, print_r
. And if you want to loop over it, you can do that as well.
在视图端,您可以简单地检索数据:要显示数据,只需使用echo、print、print_r。如果你想对它进行循环,你也可以这么做。
#9
0
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
#10
0
In controller:
控制器:
public function product(){
公共职能产品(){
$data = array("title" => "Books", "status"=>"Read","author":"arshad","company":"3esofttech",
"subject":"computer science");
“主题”:“计算机科学”);
Data From Model to controller
从模型到控制器的数据
$this->load->model('bookModel');
$result = $this->bookModel->getMoreDetailsOfBook();
**Add *$result* from model to *$data* array**
$data['tableRows'] = $result;
$data from Controller to View
$数据从控制器到视图
$this->load->view('admin/head',$data);
And to access in view file views/user.php
并访问视图文件视图/user.php
<?php echo $data;
foreach($tableRows as $row){ echo
$row['startData']; } ?>
#1
41
$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>
#2
12
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;
#3
6
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']
控制器将包含一个关联索引(不确定是否正确命名)用于数据e。g '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所以如果我有这个关于tit的arse。
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.
还有一件事——您的构造函数肯定有点浪费。所有这些库和助手的加载都是用autoload文件完成的。
#4
4
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个数据,只需注意名称并将其作为数组。
Obs²: To use the data use the key of the array with a echo.
奥林匹克广播服务公司²:使用数据使用数组的键与呼应。
#5
1
In your controller you can pass
在您的控制器中,您可以传递
$data['poll'] = "Your results";
In your view you can call
在你的视野里,你可以打电话
echo $poll;
#6
1
you can do it like this way $data['hello'] = "hello";
您可以这样做,$data['hello'] = "hello";
in view echo $hello;
鉴于echo $你好;
#7
1
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 controller do
现在,如果您有多个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 VIEW put
最后在视图中
<? echo $a["id"]; ?>
This is a big help. I figured it out by testing and it works for me.
这是一个很大的帮助。我通过测试找到了答案,它对我很有用。
#8
0
I have seen all above answer so here is what I do when I have to load the data from the controller to my view. To Pass the Data To the view from the controller:
我已经看到了上面的答案,这是我在从控制器加载数据到视图时所做的。将数据从控制器传递给视图:
public function your_controller(){
// Your Necessary Code
// You have the $data, $data2, $data3 to post to the view.
$this->load->view('your_view_directory or view_page',['data'=>$data, 'data2'=>$data2, 'data3'=>$data3... so on ]);
}
And At the View Side You can simply retrieve that data: To Display You can simply use echo
, print
, print_r
. And if you want to loop over it, you can do that as well.
在视图端,您可以简单地检索数据:要显示数据,只需使用echo、print、print_r。如果你想对它进行循环,你也可以这么做。
#9
0
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
#10
0
In controller:
控制器:
public function product(){
公共职能产品(){
$data = array("title" => "Books", "status"=>"Read","author":"arshad","company":"3esofttech",
"subject":"computer science");
“主题”:“计算机科学”);
Data From Model to controller
从模型到控制器的数据
$this->load->model('bookModel');
$result = $this->bookModel->getMoreDetailsOfBook();
**Add *$result* from model to *$data* array**
$data['tableRows'] = $result;
$data from Controller to View
$数据从控制器到视图
$this->load->view('admin/head',$data);
And to access in view file views/user.php
并访问视图文件视图/user.php
<?php echo $data;
foreach($tableRows as $row){ echo
$row['startData']; } ?>