Can We Pass Multiple Parameters To A Function In Controller? Can We Pass Multiple Parameters To A Function In Controller? codeigniter codeigniter

Can We Pass Multiple Parameters To A Function In Controller?


Multiple parameters can be passed into a controller function by adding additional URL segments. You may need to set up the route for this to happen in application/config/routes.php.

The CodeIgniter documentation mentions how to do this, and how it all works.

If that doesn't match what you are after specifically, and the parameters aren't always the same number in length, then you're going to need to use form to send the data, and use $this->input... to get the data you want to use in the function


As NetVicious mentioned,You can pass the attributes in url itself as below-

<a href="<?php echo site_url('member/addcard/'.$member->customer_id.'/'.$member->customer_mobile); ?>" class="btn-danger glyphicon glyphicon-plus"><strong style="color: #dddddd;">Add Card</strong></a>

And in the controller you can call it as-

function addcard($id,$phone) {//Your Code}

This worked fine for me.

ORyou can add data-pricetag="<?= $treatments->treatment_price?>" data-customer="<?= $member->customer_type?>" something similar like this in your input tag and call a function onlick.Then

function update_price(e) {  //  $('#treatment').on('change',function () {  var price = 0;    $(#yourid, $(e)).click(function(){     //console.log($(this).data('pricetag'));     //You get the values here     price += $(this).data('pricetag');     //price += parseInt( $(this).next().text())       var cust_type =  $(this).data('customer'); jQuery.ajax({           type: 'POST',          // dataType: 'html',          //dataType: "json",          data: your parameters,           async:true,        url: "<?php echo base_url() . 'member/memberPack/'; ?>",       success: function(data)       {           console.log(data);            //$("#price").html("");        $("#price").val(data.package_price);        $("#session").val(data.package_session);       }    });

});}

This is just a layout of my idea,you need to try out.Sorry if any mistakes.


There are 2 ways to solve your query(other than sending as uri segment):-

  1. You pass only ID ,do this thing like when you insert in your DB:-

    INSERT INTO EMPLOYEE_TABLE(employee_id, employee_name, service_id) SELECT $employee_id,(SELECT employee_name from employee_table where employee_id=$employee_id),$service_id

    1. You can submit your form through AJAX :-
("#employee_form").submit(function(e) {                     base_url=<?php your_base_url ?>                    e.preventDefault();                    $.ajax({                        type : "POST",                        url : base_url+"controller_name/function",                        data : $("#employee_form").serialize(),                        success : //do anything                    });                });

Now in your controller, you can retrieve your data by post i.e.:-

$this->input->post('employee_name');