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

时间:2022-05-27 11:01:45

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

我正在使用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);

    }

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:

在这里,我获取了员工ID和服务ID,并将它们存储在一个表中。该表包含employee_id,employee_name和service_id等字段。我的模型文件:

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

在这里,我写了一个函数,我用来插入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(); ?>

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

在这个视图中,我有一个表单,一旦我提交表单,员工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(); ?>  

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.

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

Edit 01

编辑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.

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

4 个解决方案

#1


1  

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.

可以通过添加其他URL段将多个参数传递到控制器功能。您可能需要在application / config / routes.php中设置此路由。

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

CodeIgniter文档提到了如何执行此操作以及它是如何工作的。

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

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

#2


0  

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

正如NetVicious所提到的,您可以在url中传递属性,如下所示 -

<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.

这对我来说很好。

OR you 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

或者您可以在输入标记中添加数据-picetag =“<?= $ treatments-> treatment_price?>”data-customer =“<?= $ member-> customer_type?>”类似的内容并调用函数onlick。然后

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.

这只是我的想法的布局,你需要尝试。如果有任何错误。

#3


0  

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

有两种方法可以解决您的查询(除了作为uri段发送): -

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

    你只传递ID,就像你在数据库中插入一样:

    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

    INSERT INTO EMPLOYEE_TABLE(employee_id,employee_name,service_id)SELECT $ employee_id,(从employee_table中选择employee_name,其中employee_id = $ employee_id),$ service_id

    1. You can submit your form through AJAX :-
    2. 您可以通过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');

#4


0  

I guess in your first form get the employee details. when you store that employee id you can select also employee name. and store it as the id.

我想在你的第一个表格中获取员工的详细信息。存储该员工ID时,您还可以选择员工姓名。并将其存储为id。

but if you want it another way you need to get your employee details like name, id and pass them as:

但如果你想要另一种方式,你需要获得你的员工详细信息,如姓名,身份证,并将其传递给:

$data['employee_id'] = your employee id;
$data['employee_name'] = your employee name;

in this way in your service_view.php you got two new variables.

以这种方式,在service_view.php中,您获得了两个新变量。

$employee_id : which is your employee id; $employee_name : which is your employee name.

$ employee_id:您的员工ID; $ employee_name:您的员工姓名。

then when you need to pass them to any action just pass them as any link in your site.

然后,当您需要将它们传递给任何操作时,只需将它们作为您网站中的任何链接传递即可。

site_url() . 'Controller_Name/Method_name/'.$employee_id.'/'.$employee_name;

and so on :)

等等 :)

#1


1  

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.

可以通过添加其他URL段将多个参数传递到控制器功能。您可能需要在application / config / routes.php中设置此路由。

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

CodeIgniter文档提到了如何执行此操作以及它是如何工作的。

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

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

#2


0  

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

正如NetVicious所提到的,您可以在url中传递属性,如下所示 -

<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.

这对我来说很好。

OR you 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

或者您可以在输入标记中添加数据-picetag =“<?= $ treatments-> treatment_price?>”data-customer =“<?= $ member-> customer_type?>”类似的内容并调用函数onlick。然后

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.

这只是我的想法的布局,你需要尝试。如果有任何错误。

#3


0  

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

有两种方法可以解决您的查询(除了作为uri段发送): -

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

    你只传递ID,就像你在数据库中插入一样:

    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

    INSERT INTO EMPLOYEE_TABLE(employee_id,employee_name,service_id)SELECT $ employee_id,(从employee_table中选择employee_name,其中employee_id = $ employee_id),$ service_id

    1. You can submit your form through AJAX :-
    2. 您可以通过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');

#4


0  

I guess in your first form get the employee details. when you store that employee id you can select also employee name. and store it as the id.

我想在你的第一个表格中获取员工的详细信息。存储该员工ID时,您还可以选择员工姓名。并将其存储为id。

but if you want it another way you need to get your employee details like name, id and pass them as:

但如果你想要另一种方式,你需要获得你的员工详细信息,如姓名,身份证,并将其传递给:

$data['employee_id'] = your employee id;
$data['employee_name'] = your employee name;

in this way in your service_view.php you got two new variables.

以这种方式,在service_view.php中,您获得了两个新变量。

$employee_id : which is your employee id; $employee_name : which is your employee name.

$ employee_id:您的员工ID; $ employee_name:您的员工姓名。

then when you need to pass them to any action just pass them as any link in your site.

然后,当您需要将它们传递给任何操作时,只需将它们作为您网站中的任何链接传递即可。

site_url() . 'Controller_Name/Method_name/'.$employee_id.'/'.$employee_name;

and so on :)

等等 :)