anju
anju

Reputation: 99

how i pass multiple variables from view to controller in codeigniter

I want to pass 3 variables from view to controller. how I'll pass these using site_url,

my view page

foreach ($query2 as $row2) {
    $id = $row2->id;
    $month = $row->month;
    $year = $row->year;
}
<td><a href = "/money_c/updatemanual?var1=<?php echo $id;?>&var2=<?php echo $month;?>">insert</a></td>

controller money_c

function updatemanual($id, $month, $year) {

}

How will take this variables here $id,$month,$year?

Upvotes: 1

Views: 4501

Answers (8)

jeyglo
jeyglo

Reputation: 152

Inside foreach put the variables inside an array and pass the array to the controller like bellow

foreach ($query2 as $row2) {
    $var=array(
        'id'=>$row2->id,
        'month'=>$row2->month,
        'year'=>$row2->year
    );

}
<td><a href="<?php echo base_url('money_c/updatemanual/'.$var); ?>">insert</a></td>

Upvotes: 0

You can try this:

<a href="<?php  echo base_url('test/index/'.$category_list[$i]['category_id'].'/'.str_replace(' ', '-',$category_list[$i]['category_name']));?>">

public function index($category_id = '',$category_name = '){
  echo $category_id .$category_name ;
}

Upvotes: 0

ssebetta
ssebetta

Reputation: 19

I tried this approach and it worked for me: <a href="<?php echo site_url ('money_c/update_manual/'. $id.'/'.$month.'/'.$year)>insert </a>

Upvotes: 0

shankar kumar
shankar kumar

Reputation: 648

just modify it as bellow

<?php 
    foreach($query2 as $row2){
    $id=$row2->id;
    $month=$row->month;
    $year=$row->year;
?>
    <td><a href="/money_c/updatemanual/<?php echo $id;?>/<?php echo $month;?>/<?php $year; ?> ">insert</a></td>
<?php } ?>

this will be available as $id, $month and $year varialble for that controller function

Upvotes: 0

robins
robins

Reputation: 1668

you can use this

<a href='".base_url()."money_c/updatemanual/$id/$month/$year'>insert </a>

then pass varibles through the controller function

like this way

   function updatemanual($id, $month, $year)
   {

   }

or you can use this inside the function

       $id=$this->uri->segment(3);
       $month=$this->uri->segment(4);
       $year=$this->uri->segment(5);

Upvotes: 0

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

in controller

function abc($a,$b,$c){
// access the variables here

}

in view

<td><a href="<?= base_url("/money_c/updatemanual/$id/$month/$year") ?>">insert</a></td>

While calling the view in controller , pass the values like

$data['id'] = $id;
$data['month'] = $month;
$data['year'] = $year;

Upvotes: 2

Vinie
Vinie

Reputation: 2993

User below code. Hope it will help you.

foreach($query2 as $row2) {

  $id=$row2->id;
  $month=$row->month;
  $year=$row->year;
}
 <td><a href="<?=base_url()?>/money_c/updatemanual/<?php echo $id;?>/<?php echo $month;?>/<?php echo $year;?>">insert</a></td>

Upvotes: 0

Joerg
Joerg

Reputation: 3101

Your controller should look like:

function updatemanual()
{

    $id = $this->input->get("var1");
    $month = $this->input->get("var2");

}

Upvotes: 0

Related Questions