【问题标题】:Insert array values with foreach using codeigniter使用 codeigniter 使用 foreach 插入数组值
【发布时间】:2014-06-21 10:59:31
【问题描述】:

我有一个像下面这样的表单,它动态添加和删除字段。我想要实现的是从我的表单中插入所有数据,而它已经存在多少行并不重要。

<tr>
<td><input name="model[]" type="text" class="input-large" id="A1" value="" /></td>
<td><input name="color[]" type="text" class="input-small" id="Color1" value="" /></td>
<td><input name="price[]" type="text" class="input-small" id="B1" value="" data-format="0,0[.]00" /></td>
<td><input name="quantity[]" type="text" class="input-small" id="C1" value="" data-format="0" /></td>
<td><input name="addon[]" type="text" class="input-small" id="D1" value="" data-format="0,0[.]00" /></td>
<td><input name="discount[]" type="text" class="input-small" id="E1" value="" data-format="0,0[.]00" /></td>
<td><input name="total[]" type="text" class="input-small" id="F1" value="" data-formula="($B1*$C1)+($D1-$E1)" readonly /></td>
<td><input type="button" value="remove" class="btn-remove btn btn-mini btn-danger" /></td>
</tr>

javascript:

<script type="text/javascript">
var currentRow = 1;
$(document).ready(function(){
    $('#calx').calx();

    $('#add_item').click(function(){
        var $calx = $('#calx');
        currentRow++;

        $calx.append('<tr>\
            <td><input type="text" name="model[]" class="input-large" id="A'+currentRow+'" value="" /></td>\
            <td><input type="text" name="color[]" class="input-small" id="Color1'+currentRow+'" value="" /></td>\
            <td><input type="text" name="price[]" class="input-small" id="B'+currentRow+'" value="" data-format="0,0[.]00" /></td>\
            <td><input type="text" name="quantity[]" class="input-small" id="C'+currentRow+'" value="" data-format="0" /></td>\
            <td><input type="text" name="addon[]" class="input-small" id="D'+currentRow+'" value="" data-format="0,0[.]00" /></td>\
            <td><input type="text" name="discount[]" class="input-small" id="E'+currentRow+'" value="" data-format="0,0[.]00" /></td>\
            <td><input type="text" name="total[]" class="input-small" id="F'+currentRow+'" value="" data-format="" data-formula="($B'+currentRow+'*$C'+currentRow+'+$D'+currentRow+'-$E'+currentRow+')" /></td>\
            <td><input type="button" value="remove" class="btn-remove btn btn-mini btn-danger" /></td>\
        </tr>');

        //update total formula
        $('#G1').attr('data-formula','SUM($F1,$F'+currentRow+')');
        $calx.calx('refresh');
    });

    $('#calx').on('click', '.btn-remove', function(){
        $(this).parent().parent().remove();
        $('#calx').calx('refresh');
    });
});
</script>

我有一个类似的控制器:

    public function add_transactions(){
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $this->form_validation->set_rules('model[]', 'Item Name or Model', 'trim|required|xss_clean');
    $this->form_validation->set_rules('color[]', 'Color', 'trim|required|xss_clean');
    $this->form_validation->set_rules('price[]', 'Item Price', 'trim|required|xss_clean');
    $this->form_validation->set_rules('quantity[]', 'Quantity', 'trim|required|xss_clean');
    $this->form_validation->set_rules('addon[]', 'Add-on', 'trim|required|xss_clean');
    $this->form_validation->set_rules('discount[]', 'Discount', 'trim|required|xss_clean');
    $this->form_validation->set_rules('total[]', 'Unit cost or Amount', 'trim|required|xss_clean');


    if($this->form_validation->run() == FALSE){

        $code = $this->input->post('code');

        $this->load->model('purchase_order');
        $this->load->model('supplier');
        $data['result'] = $this->purchase_order->getPurchaseOrderByID($code);
        $data['results'] = $this->supplier->SupplierList();
        $data['main_content'] = 'purchase_order_pricing.php';
        $this->load->view('dashboard',$data);                       

    } else {

        $this->load->model('purchase_order');
        $result = $this->purchase_order->AddPurchaseOrder($data);
        if(!$result['error'])
        {
            $this->session->set_flashdata('flashSuccess', 'Purchase Order transaction has been updated.');
            redirect('home/purchase_order_view', 'refresh');
            $data['main_content'] = 'purchase_order_view.php';
            $this->load->view('dashboard',$data);                       
        } 
        else 
        {
            $this->session->set_flashdata('flashSuccess', 'The server is busy please try again later.');
            redirect('home/purchase_order_view', 'refresh');
            $data['main_content'] = 'purchase_order_view.php';
            $this->load->view('dashboard',$data);       
        }               
    }       

}

还有一个模型:

public function AddPurchaseOrder($data){



$data =array();
    for($i=0; $i<sizeof($data); $i++) {
        $data[$i] = array(
            'model' => $model[$i], 
            'color' => $color[$i],
            'price' => $price[$i],
            'quantity' => $quantity[$i],
            'addon'=>$addon[$i],
            'discount'=>$discount[$i],
            'total'=>$total[$i],
            'code'=>$code[$i] 
        );
    }
$this->db->insert('purchase_order_info',$data);
}

【问题讨论】:

    标签: php codeigniter foreach


    【解决方案1】:
    public function AddPurchaseOrder ($data)
    {
        $data =array();
        for($i=0; $i < $count; $i++) 
        {
            $data[$i] = array(
            'model' => $model[$i], 
            'color' => $color[$i],
            'price' => $price[$i],
            'quantity' => $quantity[$i],
            'addon'=>$addon[$i],
            'discount'=>$discount[$i],
            'total'=>$total[$i] 
            );
        }
    
        $this->db->insert_batch('table_name_here', $data);
    }
    

    【讨论】:

    • 嗨@Aishish我收到错误您必须使用“设置”方法来更新条目
    • 对不起,我再次收到错误消息:使用未定义的常量 i - 假定为 'i'
    • 使用 $data[$i];而不是 $data[i]
    • 消息:未定义变量:数据对不起,但我现在很困惑
    • 发布你使用的新代码,我会帮你改正
    【解决方案2】:

    只是想为我的问题发布我的解决方案以供将来关注。这可能会帮助任何在 insert_batch() 中遇到问题或想知道如何在 db 中使用 insert_batch() 记录的人。

    在我的模型中...

    $model = $this->input->post('model');
    $color = $this->input->post('color');
    $price = $this->input->post('price');
    $quantity = $this->input->post('quantity');
    $addon = $this->input->post('addon');
    $discount = $this->input->post('discount');
    $total = $this->input->post('total');
    
    $orders = array();
    
    for ($i=0; $i < count($model); $i++) { 
    
            $orders[] = array( 'id'=>null,
            'model' => $model[$i],
            'color' => $color[$i],
            'price' => $price[$i],
            'quantity' => $quantity[$i],
            'addon' => $addon[$i],
            'discount' => $discount[$i],
            'total' => $total[$i]   
            );
    
        } 
    
    $this->load->model('purchase_order');
    $result = $this->purchase_order->AddPurchaseOrder($orders);
    

    在我的模型中

    public function AddPurchaseOrder($orders){
    
    $this->db->insert_batch('purchase_order_info', $orders); 
    print '<pre>';
    print_r($orders);
    print '</pre>';
    die();
    }
    

    希望对大家有帮助。

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 2010-12-21
      • 2018-09-16
      相关资源
      最近更新 更多