我们可以将多个参数传递给控制器​​中的功能吗?

Anu*_*Anu 14 php mysql arguments controller codeigniter

我正在使用codeigniter.我有一个视图文件,我选择一个员工并使用表单操作发送id并选择员工可以执行的服务并将它们存储在一个表中.我已经插入了员工ID和服务ID.现在我需要插入员工姓名.
我的控制器文件是

 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);

    }
Run Code Online (Sandbox Code Playgroud)

在这里我得到了员工ID和服务ID并将它们存储在一个table.That表包含像场employee_id,employee_nameservice_id.
我的模型文件:

class sample_model extends CI_Model{

 function store_service($data)
    {
        $insert = $this->db->insert('service', $data);
        return $insert;
}
}
Run Code Online (Sandbox Code Playgroud)

在这里,我写了一个函数,我用来插入addservice表中.我将所有数据存储为数组并将其保存在addservice表中.
我的视图文件,我选择一名员工. 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(); ?>
Run Code Online (Sandbox Code Playgroud)

在这个视图中,我有一个表单,一旦我提交表单员工ID将被发送到控制器功能,这里我需要发送员工姓名..可以有人帮我代码吗?
我选择服务的另一种观点是:
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(); ?>  
Run Code Online (Sandbox Code Playgroud)

上面的视图调用我的控制器文件中的services函数,通过它我可以保存数据,例如第一个视图中的employee id和第二个视图中的services id.

编辑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);  

    }
Run Code Online (Sandbox Code Playgroud)

这是一个函数,我将employee id作为会话变量,并在我上面的选择函数中使用它们.是否有可能获得员工姓名,就像我有employee_id一样.

gab*_*886 1

通过添加额外的 URL 段,可以将多个参数传递到控制器函数中。您可能需要在 application/config/routes.php 中设置路由才能发生这种情况。

CodeIgniter文档提到了如何做到这一点,以及它是如何工作的。

如果这与您具体想要的不匹配,并且参数的长度并不总是相同,那么您将需要使用 form 来发送数据,并使用来$this->input...获取您想要使用的数据在函数中