dongxi3911
2011-06-26 12:46
浏览 64
已采纳

如何将视图变量传递给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. I would appreciate any help on this.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongzouche9108 2011-06-26 12:48
    已采纳

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dqe9657 2011-06-26 15:50

    you should not call the $id from the View, you should get it at the controller level and pass it to the View.

    as Bulk said. you URL will be something like that 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.

    you will have your url like this: www.mysite.com/home/show_id/id

    your function in home will read the 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):

    <?php echo $id ?>
    

    nothing else..

    hope this helps.

    评论
    解决 无用
    打赏 举报
  • duankao4489 2014-04-02 15:13

    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

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

    in my next view daftarmember_form.html

    )

    <?php echo $id;?>
    

    they print mantap,

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题