Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can We Pass Multiple Parameters To A Function In Controller?

I am using codeigniter. I have a view file where i choose a employee and send the id using form action and select services that a employee can perform and store them in a table.I have inserted the employee id and service id. Now i need to insert employee name.
My controller file is

 class admin_service_limitation extends CI_Controller{
    public function services()
        {
    $id = $this->session->userdata('employee');
    $id_array = $this->input->post("services");
//I need to get the employee name here and store them in a table along with employee id and service id
    for ($i = 0; $i < count($id_array); $i++) {
        if(isset($id_array[$i])&& $id_array[$i]!=""){
        $data_to_store = array('employee_id' => $id, 'service_id' => $id_array[$i]);
        $this->add_service_model->save($data_to_store);
       }
    }
    $data['addservice'] = $this->add_service_model->get_addservices();
    $data['main_content'] = 'admin/service_limitation/newview';
    $this->load->view('includes/template', $data);

    }

Here i am getting the employee id and service id and store them in a table.That table contains fields like employee_id,employee_name and service_id.
My model file:

class sample_model extends CI_Model{

 function store_service($data)
    {
        $insert = $this->db->insert('service', $data);
        return $insert;
}
}

Here i wrote a function that i use to insert into the addservice table. I get all the data to be stored as array and save them in the addservice table.
My view file where i choose a employee. select.php

 <?php          
      echo form_open('admin/service_limitation/service_view/'.$this->uri->segment(4).'', $attributes);
      ?>

     <table class="table table-striped table-bordered table-condensed">
            <thead>
              <tr>
                <th class="header">Employee id</th>
                <th class="yellow header headerSortDown">First name</th>
                <th class="green header">Last name</th>
                <th class="red header">Email</th>
                <th class="red header">Chair renter</th>

                <th class="red header">Actions</th>
              </tr>
            </thead>
            <tbody>
              <?php
              foreach($employee as $row)
              {
                echo '<tr>';
                echo '<td>'.$row['id'].'</td>';
                echo '<td>'.$row['emp_first_name'].'</td>';
                echo '<td>'.$row['emp_last_name'].'</td>';
                echo '<td>'.$row['emp_email_id'].'</td>';
                                echo '<td>'.$row['chair_renter'].'</td>';



                echo '<td class="crud-actions">
                  <a href="'.site_url("admin").'/service_limitation/service_view/'.$row['id'].'" class="btn btn-info">Select employee</a>  

                </td>';
                echo '</tr>';
              }
              ?>      
            </tbody>
          </table> 
<?php echo form_close(); ?>

In this view i have a form once i submit the form employee id will be sent to the controller function, Here i need to send employee name now.. Can some one help me code?
Another view where i choose services is:
service_view.php

            <?php

            $attributes = array('class' => 'form-inline reset-margin', 'id' => '');
 echo form_open('admin/service_limitation/newview', $attributes);

            ?>             
          <table class="table table-striped table-bordered table-condensed">
            <thead>
              <tr>
                <th class="header">Service id</th>
                <th class="yellow header headerSortDown">Service name </th>
                <th class="green header">Service catogary</th>
                <th class="red header">Service tax</th>
                <th class="red header">Service length</th>
                <th class="red header">Service price</th>
                <th class="red header">Actions</th>
              </tr>
            </thead>
            <tbody>
              <?php
              foreach($service as $row)
              {
                echo '<tr>';
                echo '<td>'.$row['id'].'</td>';
                echo '<td>'.$row['service_name'].'</td>';
                echo '<td>'.$row['category'].'</td>';
                echo '<td>'.$row['service_tax'].'</td>';
                echo '<td>'.$row['service_length'].'</td>';
                echo '<td>'.$row['service_price'].'</td>';
                echo '<td class="crud-actions">
                 <input type="checkbox" value="'.$row['id'].'" name="services[]"/>

                </td>';
                echo '</tr>';
              }
              ?>      
            </tbody>
          </table>
    <button class="btn btn-primary" type="submit">Save changes</button>
            <button class="btn" type="reset">Cancel</button>
    </div>
      <?php echo form_close(); ?>  

The above view calls the services function in my controller file by which I can save data such as the employee id from the first view and services id from the second view.

Edit 01

public function select_service($id)
    {
        $this->load->library('session');
         $this->session->set_userdata('employee', $id);

    $data['service'] =   $this->add_service_model->get_services();  
            $data['main_content'] = 'admin/service_limitation/service_view';
        $this->load->view('includes/template', $data);  

    }

This is a function where i get the employee id as session variable and use them in selection function i have above. is it possible to get the employee name like I got employee_id.

like image 453
Anu Avatar asked Jul 06 '15 04:07

Anu


People also ask

Can a function have multiple parameters?

Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

How do I pass multiple parameters to a route?

Passing Multiple Parameters Using Route to Controller In this example, will first define a route with multiple parameters and then we will add a controller method accepting multiple parameters. Then we will setup a link with named route having multiple parameters.

How pass multiple parameters from view to controller in MVC?

First of all either you need to create custom route or enable MapMvcAttributeRoutes in your routeconfig file by adding below line of code. routes. MapMvcAttributeRoutes(); Then in your controller above your defined action add something like below.


1 Answers

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

like image 118
gabe3886 Avatar answered Sep 30 '22 11:09

gabe3886