簡體   English   中英

如何將視圖變量傳遞給Codeigniter中的控制器?

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

我的觀點 :-

 <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>

我想在控制器中使用變量$id 。我正在使用codeigniter,並且是初學者。 我將不勝感激。

您不應從View調用$ id,而應在控制器級別獲取它並將其傳遞給View。

正如Bulk所說。 您的網址將類似於www.mysite.com/thecontrollername/thefunction/id

例如,如果您的控制器位於home且其中包含show_id函數,則您的視圖稱為show_id_view.php。

您將擁有如下網址:www.mysite.com/home/show_id/id

您在家中的功能將讀取ID”

在您的家庭控制器中:

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

在視圖(show_id_view)中:

<?php echo $id ?>

沒有其他的..

希望這可以幫助。

在我看來,我添加了鏈接

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

在我的link.php控制器中,我添加了函數show_id

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

在我的下一個視圖中daftarmember_form.html

<?php echo $id;?>

他們打印出mantap,

好吧,理想情況下您不會這樣做。 如果需要在控制器中使用變量,應首先在控制器中分配該變量並將其傳遞給視圖。

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

$ id也將在您看來可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM