[英]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.