简体   繁体   English

如何发送单个变量以在Codeigniter中查看

[英]How to send single variable to view in codeigniter

Hi I want to send single variable to view but I am unable to do it, Please help me. 嗨,我想发送单个变量进行查看,但我无法执行此操作,请帮助我。

Controller: 控制器:

public function src($id=""){
    $this->load->view('catsrc', $id);
}

View: 视图:

<div class="container">
<?php echo $id ;?>
</div>

Error Getting: 错误获取:

Message: Undefined variable: id 消息:未定义的变量:id

I am not an expert of CodeIgniter but according to official documentation : "Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function" 我不是CodeIgniter的专家,但是根据官方文档“数据是通过视图加载函数的第二个参数中的数组对象从控制器传递到视图的”

$this->load->view('catsrc', [ 'id' => $id ]);

was that so hard ? 这么难吗?

Happy coding :) 快乐的编码:)

CodeIgniter views read from a $data array. 从$ data数组读取的CodeIgniter视图。 To reference $id in a view, put it in the $data array in your controller 要在视图中引用$ id,请将其放入控制器的$ data数组中

$data = array('id' => 'your_value_here');

and pass $data to the view. 并将$ data传递给视图。

Full documentation on views lives at: https://ellislab.com/codeigniter/user-guide/general/views.html 有关视图的完整文档,请访问: https : //ellislab.com/codeigniter/user-guide/general/views.html

How CI Classes Pass Information and Control to Each Other CI类如何相互传递信息和控制权

Calling Views We have seen how the controller calls a view and passes data to it: First it creates an array of data ($data) to pass to the view; 调用视图我们已经看到了控制器如何调用视图并将数据传递给它:首先,它创建一个数据数组($ data)传递给视图。 then it loads and calls the view in the same expression: 然后它以相同的表达式加载并调用视图:

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

You can call libraries, models, plug-ins, or helpers from within any controller, and models and libraries can also call each other as well as plug-ins and helpers. 您可以从任何控制器中调用库,模型,插件或帮助程序,并且模型和库也可以相互调用,也可以调用插件和帮助程序。 However, you can't call one controller from another, or call a controller from a model or library. 但是,您不能从另一个控制器调用一个控制器,也不能从模型或库调用控制器。 There are only two ways that a model or a library can refer back to a controller: Firstly, it can return data. 模型或库只能通过两种方式引用回控制器:首先,它可以返回数据。 If the controller assigns a value like this: 如果控制器分配这样的值:

$foo = $this->mymodel->myfunction();

and the function is set to return a value, then that value will be passed to the variable $foo inside the controller. 并且将函数设置为返回值,然后将该值传递到控制器内部的变量$ foo。

This may not answer your question directly but helps!! 这可能不会直接回答您的问题,但会有所帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM