【问题标题】:Can We Pass Multiple Parameters To A Function In Controller?我们可以将多个参数传递给控制器​​中的函数吗?
【发布时间】:2015-09-23 02:53:30
【问题描述】:

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

    }

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

class sample_model extends CI_Model{

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

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

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

上面的视图调用了我的控制器文件中的服务函数,通过它我可以保存数据,例如第一个视图中的员工ID和第二个视图中的服务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);  

    }

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

【问题讨论】:

  • 看不懂问题
  • 我需要发送我的员工姓名和员工 id.here,选择员工
  • .$row['id'] 中它将所有数据传递到表中,那么您如何选择一个?否则使用下拉菜单
  • 如果它通过了所有数据,我可以在这里获取员工姓名吗?
  • 没办法。或先提示文本框输入emp编号或名称。(必须唯一)。当用户输入正确的加载相关员工数据时。

标签: php mysql codeigniter controller arguments


【解决方案1】:

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

但如果您想要另一种方式,您需要获取您的员工详细信息,例如姓名、ID 并将它们传递为:

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

以这种方式在你的 service_view.php 你有两个新变量。

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

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

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

等等:)

【讨论】:

    【解决方案2】:

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

    1. 您只传递 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

      1. 您可以通过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
                        });
                    });
    

    现在在您的控制器中,您可以通过邮寄方式检索您的数据,即:-

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

    【讨论】:

      【解决方案3】:

      正如 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>
      

      而在控制器中,你可以称它为-

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

      这对我来说很好。

      或 您可以在输入标签中添加类似这样的data-pricetag="&lt;?= $treatments-&gt;treatment_price?&gt;" data-customer="&lt;?= $member-&gt;customer_type?&gt;" 并调用函数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);
      
             }
          });
      

      }); }

      这只是我的想法的一个布局,你需要尝试。如有错误,请见谅。

      【讨论】:

      • 用 url 传递这么大的值是个好习惯吗?
      • 如果这不是秘密凭据,您可以按照此操作,或者您可以使用 ajax 并传递数据。
      • ajax?怎么做。你能帮帮我吗?
      • 我已经添加了答案,如果有任何错误,请见谅。这只是我的一个想法。
      • 首选第一种方法,因为简单,容易,代码少。
      【解决方案4】:

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

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

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

      【讨论】:

      • 我只需要知道如何将员工姓名、地址、电话号码等多个值从视图文件传递给控制器​​功能。 选择员工 在这段代码中怎么可能所有需要的值
      • 要按照您的要求进行操作,您只需要将 URL 变大以采用 `controller/function/name/address/phone number 格式
      • 在这个 选择员工 我需要将员工姓名连同emp_id..site_url("admin").'/service_limitation/service_view/'.$row['id']..如何在这个地方添加它一起发布
      • 从您所说的来看,我认为有两种选择:1)当您转到页面并传递用户 ID 时,然后从数据库中加载详细信息(如果它们已经存在)和将它们存放在其他地方。如果您要发送新的详细信息,则每个链接行都应该是自己的表单,以便可以提交详细信息。
      • 你不需要表格。将更多元素添加到您在网页上生成的 链接中的 url,例如:选择员工
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      相关资源
      最近更新 更多