由 implode 定义的多维数组插入 sql table 的同一列的所有数据

Multidimensional array defined by implode inserts all data same column of sql table

错误

在控制器中定义数组,然后转换为使用内爆功能的模型。

问题 = 数据插入在sqltable的同一列中。例如:Coke,Pepsi,Slice(1 列)

t_id| product_name | 单位 | 费用

1 | 可口可乐、百事可乐、Slice | 5 | 1000

需要解决方案 = 数据应插入多个列中,例如

t_id| product_name | 单位 | 费用

1 | 可乐 | 5 | 1000

2 | 百事可乐 | 2 | 500

3 | 切片 | 3 | 600

查看

<div class="col-lg-6">

    <?php
        $getNameValue = $this->session->userdata('nameValue');
        echo '<h3>' .$getNameValue['name'] .' paid Rs ' .$getNameValue['cash_amount']. '</h3>';
        $getValue = $this->session->userdata('sessiondata');
        if ($getValue != NULL){

            echo '<table  class="table table-bordered table-striped">';
            echo '<tr>';
            echo '<td><strong>Product Name</strong></td>';
            echo '<td><strong>Unit</strong></td>';
            echo '<td><strong>Cost</strong></td>';
            echo "<form method='post' action=''>";
            foreach ($getValue as $row)
            {
                echo '<tr>';
                echo '<td><input type="hidden" name="allProduct[product_name][]" value="'.$row['product_name'].'">' .$row['product_name']. '</td>';
                echo '<td><input type="hidden" name="allProduct[unit][]" value="'.$row['unit'].'">' .$row['unit'].'</td>';
                echo '<td><input type="hidden" name="allProduct[cost][]" value="'.$row['cost'].'">' .$row['cost'].'</td>';
                echo '</tr>';
            }
            echo '<tr><td></td><td><td><button type="submit" class="btn btn-success">POST</button></td></td></tr>';
            echo "</form>";
            echo '</table>';
        }

     ?>

</div>

控制器

public function newSystemBatch($getBatch)
{
    if ($data = $this->input->post('systemProduct')) {

        $data['batch_id'] = $getBatch;

        if (isset($data['sum'])) {
            $data['cost'] = $data['cost'] / $data['unit'];
            unset($data['sum']);
        }

        $productName = $data['product_name'];
        $unit = $data['unit'];
        $cost = $data['cost'];

        $getValue = $this->session->userdata('sessiondata');
        $getValue[] = array(
            'product_name'  => $productName,
            'unit'     => $unit,
            'cost' => $cost

        );
        $this->session->set_userdata('sessiondata', $getValue);


        $this->session->set_flashdata('message', $data);

        redirect('inventory/newSystemMessage/' . $getBatch['batch_id']);

    }

    if ($data = $this->input->post('allProduct')) {
        $this->header();
        $this->footer();
        $this->transaction->addNew($data);
        $this->load->view("admin/newSystemTable", $data);
    }

    else {
        $this->header();
        $data['action'] = 'System';
        $data['users_id'] = $this->user->users_id;
        $this->load->view("admin/newSystemTable", $data);
        $this->footer();
    }
}

型号

function addNew($data)
{
    if(is_array($data['product_name'])) $data['product_name'] = implode(",", $data['product_name']);
    if(is_array($data['unit'])) $data['unit'] = implode(",", $data['unit']);
    if(is_array($data['cost'])) $data['cost'] = implode(",", $data['cost']);

    $this->db->insert('try', $data);

}

型号:

function addNew($data)
{
    // Test that all all sub arrays are equal size
    $length = array_unique(array_map('count', $data));
    if (count($length) == 1) {
       $length = current($length);
       $keys = array_keys($data);
       $res = array();
       for($i = 0; $i < $length; $i++) 
          foreach($keys as $key)
             $res[$i][$key] =  $data[$key][$i];
       $this->db->insert_batch('try', $res);
       }
    else {
        // incorrect data
        return false;
        }
    return true;
}