【问题标题】:Insert array posts in code igniter在 codeigniter 中插入数组帖子
【发布时间】:2015-11-14 14:55:14
【问题描述】:

我必须将数据插入到表中,如下所示: 在一个请求中,有许多人将被包含在请求中。 现在,我完成了插入请求信息。 我在将人员信息与请求 ID 一起插入 request_details 表时遇到问题。

例如:

在请求表中:

Request table
Request ID  |  Description
1           |  sample1


Request Details
Req_det_id    |   Request ID  |  Person    |Quantity
1             |     1         |  P1        | 10
2             |     1         |  P2        |20

如何实现将数组插入到请求详细信息表中?

在我看来:

<div id="show" style="display:none;">
    <select name="hhname[]" value="" id="drop2"  style="color:black;">
            <?php
                foreach ($head as $row) {
                    echo "<option style='color:black;' value='".$row['hh_id']."'>".$row['hh_fname']."  ".$row['hh_lname']."</option>";
                                            }
                          ?>
    </select>
    <input type="text" placeholder="Quantity" name="quant[]" value="">
</div>

在我的控制器中:

$req_data = array(
            'requestor_id' => $input['requestor_id'],
            'request_package_id' => $r_id,
            'request_place' => $input['request_place'],
            'request_date' => $input['request_date'],
            'request_remarks' => $input['request_remarks'],
            'request_specify' => $input['request_specify'],
            'request_quantity' => $input['request_quantity'],
            'request_status' => 1,
            'request_code' => "fsdf2324",

        );
        $request_id = $this->CreateRequest_model->addReq($req_data);
        $this->CreateRequest_model->updatePackTable($input['package_id']);

        $name = $input['hhname']; // this is already an array
        $quant = $input['quant']; // this is an array

        foreach ($name as $row ) {
            $data = array(
                //how should i insert it to the req details table?
            );
        }

非常感谢您的帮助。

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    创建一个新的关联数组并将该关联数组推送到不同的数组中,然后使用 this->db->insert_batch('table_name', batch_Array to be insert)。

    例如。 控制器:

    $name = $input['hhname']; // this is already an array
    $quant = $input['quant']; // this is an array
    $cnt = count($name);
    $cnt1 = count($quant);
     $push_arr = array();
    for($i=0; $i<$cnt && $i<$cnt1; $i++)
    {
      $tmp_arr = array('$request_id' => $request_id,
                       'Person'=> $name[$i],
                       'Quantity'=>$quant[$i]);
     array_push($push_arr,$tmp_arr);
    }
    $this->Your_model->insert_request_details($push_arr);
    

    型号:

    function insert_request_details($push_arr)
    {
       $val = $this->db->insert_batch('Request Details',$push_arr);
       return val;
    }
    

    【讨论】:

    • 哇!有效!非常感谢,兄弟,你拯救了我的一天。
    猜你喜欢
    • 2019-03-14
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2013-12-16
    • 2021-03-07
    相关资源
    最近更新 更多