Arvind
Arvind

Reputation: 179

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. I would appreciate any help on this.

Upvotes: 0

Views: 10283

Answers (3)

KEKUATAN
KEKUATAN

Reputation: 948

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,

Upvotes: 0

Antony P.
Antony P.

Reputation: 143

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.

Upvotes: 1

Dan Smith
Dan Smith

Reputation: 5685

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.

Upvotes: 0

Related Questions