[英]Passing variable to view from controller
Can something like this be done? 可以这样做吗? I want to pass a variable from a public function to my view.
我想将变量从公共函数传递到我的视图。
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->variable;
$this->load->view('home_view', $home_data);
}
public function a_function() {
public $variable = "cool";
}
//EDIT// //编辑//
This is what I m actually trying to accomplish and I m stuck. 这是我实际上想要完成的工作,我陷入了困境。
get_two
gets two items from a table. get_two
从表中获取两个项目。 I want to add the two items to two variables and pass them to the view. 我想将两个项目添加到两个变量,并将它们传递给视图。
public function get_two() {
$get_results = $this->home_model->get_two_brands();
if($get_results != false){
$html = '';
foreach($get_results as $result){
$html .= '<li><a href="#" class="pick" id="'.$result->id.'">'.$result->brand.'</a></li>';
}
$result = array('status' => 'ok', 'content' => $html);
header('Content-type: application/json');
echo json_encode($result);
exit();
}
}//public function get_two() {
Should I create two functions like this? 我应该创建两个这样的函数吗? But I don't know how to pass the
$get_results
array from get_two
to the below functions. 但是我不知道如何将
$get_results
数组从get_two
传递给以下函数。 I tried public $get_results = $this->model ... etc
but that didn't work. 我尝试了
public $get_results = $this->model ... etc
但是没有用。
public function result_one() {
return $resultOne = $get_results[0];
}
public function result_two() {
return $resultTwo = $get_results[1];
}
I'm not sure I've got the question correctly but what you're trying to achieve is something like this? 我不确定我的问题是否正确,但是您要达到的目标是这样的?
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->a_function();
$this->load->view('home_view', $home_data);
}
public function a_function() {
return $variable = "cool";
} }
/ ** AFTER EDIT ** / / **编辑后** /
Things get complicated (possibly because of my english comprehension). 事情变得复杂(可能是由于我的英语理解能力)。
you said 你说
get_two gets two items from a table.
get_two从表中获取两个项目。 I want to add the two items to two variables and pass them to the view.
我想将两个项目添加到两个变量,并将它们传递给视图。
So from the function get_two()
you need to get and use the result in this way? 因此,您需要从函数
get_two()
以这种方式获取和使用结果吗?
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->get_two(); // <- here?
$this->load->view('home_view', $home_data);
}
So you can try with: 因此,您可以尝试:
public function get_two() {
$get_results = $this->home_model->get_two_brands();
if($get_results != false){
return $get_results;
}
}
and then 接着
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->get_two();
$this->load->view('home_view', $home_data);
}
and inside you home_view : 在你里面home_view:
<?php
foreach($home_data['cool'] as $result){
echo '<li><a href="#" class="pick" id="'.$result->id.'">'.$result->brand.'</a></li>';
}
?>
/ ** AFTER NEW QUESTION ** / / **提出新问题后** /
I need the ids of the two choices as two distinct variables
我需要两个选择的id作为两个不同的变量
So change the index function
this way: 因此,以这种方式更改
index function
:
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->get_two(); // <- maybe you don't need this anymore
list($result1, $result2) = $this->get_two();
$home_data['resultId1'] = $result1->id;
$home_data['resultId2'] = $result2->id;
$this->load->view('home_view', $home_data);
}
Now you're able to use $home_data['resultId1']
and $home_data['resultId1']
inside your view. 现在,您可以在视图内使用
$home_data['resultId1']
和$home_data['resultId1']
。
I don't know the codeigniter framework so this is why I asked for a part of your view but it looks pretty simple as I check in the doc. 我不知道codeigniter框架,所以这就是为什么我要求您提供部分视图的原因,但是当我签入文档时,它看起来非常简单。 And the doc's are not bad there.
而且文档在那里也不错。
Check for Adding Dynamic Data to the View 检查是否将动态数据添加到视图
public_function()
should return something, for ex: public_function()
应该返回一些东西,例如:
function public_function() {
return 'groovy';
}
Then call it in the controller: 然后在控制器中调用它:
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->public_function();
$this->load->view('home_view', $home_data);
}
Then add to the view somewhere <?php echo $home_data['cool'];?>
然后添加到视图中的某处
<?php echo $home_data['cool'];?>
I assume it's wrapped in some class. 我认为它被包裹在某个类中。 So if you cannot
return
the value you need (for ex. function already returns something else) then do something like this: 因此,如果您无法
return
所需的值(例如,函数已经返回了其他值),请执行以下操作:
class Someclass {
public $some_class_variable;
function public_function() {
$this->some_class_variable = 'groovy';
}
function index() {
$home_data['cool'] = $this->some_class_variable;
}
}
You can also define the variable in the constructor, this is one way . 您也可以在构造函数中定义变量,这是一种方法。
CODE: 码:
public function __construct(){
$this->variable = "cool";
}
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->variable;
$this->load->view('home_view', $home_data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.