简体   繁体   English

如何将视图变量传递给Codeigniter中的控制器?

[英]how do i pass a view variable to the controller in codeigniter?

My View :- 我的观点 :-

 <html>
<?= link_tag(base_url().'css/simple.css'); ?>
<body>
<?php $this->load->helper('form'); ?>
<?php $this->load->view('commentform'); ?>
<?php $id=$this->uri->segment(3);?>
<?php echo $id;?>
</body>
</html>

i would like to use the variable $id in my controller.I'm using codeigniter by the way, and am a beginner. 我想在控制器中使用变量$id 。我正在使用codeigniter,并且是初学者。 I would appreciate any help on this. 我将不胜感激。

you should not call the $id from the View, you should get it at the controller level and pass it to the View. 您不应从View调用$ id,而应在控制器级别获取它并将其传递给View。

as Bulk said. 正如Bulk所说。 you URL will be something like that www.mysite.com/thecontrollername/thefunction/id 您的网址将类似于www.mysite.com/thecontrollername/thefunction/id

for example your controller if home and there is a show_id function in it and your view is call show_id_view.php. 例如,如果您的控制器位于home且其中包含show_id函数,则您的视图称为show_id_view.php。

you will have your url like this: www.mysite.com/home/show_id/id 您将拥有如下网址:www.mysite.com/home/show_id/id

your function in home will read the id" 您在家中的功能将读取ID”

in your home controller: 在您的家庭控制器中:

function show_id(){
$id=$this->uri->segment(3);
$view_data['id'] = $id;
$this->load->view('show_id_view',$view_data);  
}

in the view (show_id_view): 在视图(show_id_view)中:

<?php echo $id ?>

nothing else.. 没有其他的..

hope this helps. 希望这可以帮助。

in my view i add link 在我看来,我添加了链接

 <li><a href="<?php echo base_url()?>link/show_id/mantap"> coba </li> 

in my link.php controler i add function show_id 在我的link.php控制器中,我添加了函数show_id

function show_id(){
        $id=$this->uri->segment(3);
        $data['coba'] = $id;
        $this->mobile->view('**daftarmember_form**',$data);

in my next view daftarmember_form.html 在我的下一个视图中daftarmember_form.html

)

<?php echo $id;?>

they print mantap, 他们打印出mantap,

Well, ideally you wouldn't do it this way. 好吧,理想情况下您不会这样做。 You should assign the variable first in the controller and pass it to the view if you need to use it there. 如果需要在控制器中使用变量,应首先在控制器中分配该变量并将其传递给视图。

$data['id'] = $this->uri->segment(3);
$this->load->view('view_file', $data); 

$id would then be available in your view as well. $ id也将在您看来可用。

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

相关问题 如何将变量从控制器传递到Codeigniter中的视图? - How do I pass a variable from the controller to the view in Codeigniter? 如何在codeigniter中将变量从视图传递到控制器 - How to pass a variable from view to controller in codeigniter CodeIgniter如何将视图中的变量传递给控制器 - CodeIgniter How to pass a variable from view to the controller 如何根据我在CodeIgniter中获得的变量将变量从控制器传递到视图,以及如何显示视图? - How to pass the variable from controller to view and how to display view according to the variable I got in CodeIgniter? 我无法从控制器传递变量以在Codeigniter中查看 - I cant pass variable from controller to view in Codeigniter 如何在视图中将变量的值从视图传递到控制器 - How to pass a value of a variable from view to controller in codeigniter 如何在Codeigniter中使用Ajax将变量从Controller传递到View? - How to pass variable from Controller to View with Ajax in Codeigniter? 无法将变量从codeigniter控制器传递到视图? - Unable to pass variable from codeigniter controller to the view? CodeIgniter从控制器传递变量到视图 - CodeIgniter pass variable from controller to view 我如何将多个变量从视图传递到Codeigniter中的控制器 - how i pass multiple variables from view to controller in codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM