如何使用Codeigniter插入循环数据

时间:2019-01-26 11:12:41

标签: php codeigniter

我想插入循环100条以上记录的数据

但是我不知道如何使用codeigniter插入循环数据。

此控制器

public function save()
    {

        if($this->input->post('code') && $this->input->post('amount'))
        {               
            $data = array(
        'code' => $this->input->post('code'),
        'amount' => $this->input->post('amount'),
                'admin_id' => logged('id')
            );

      $rs = $this->voucher_model->add($data);

      echo $rs === TRUE ? 'success' : $this->voucher_model->error;
        }

    }

此模型课程

Class Voucher_model extends MY_Model
    {

    public $id;
    public $code;
    public $amount = 0;

    public $date_upd;
    public $error;
    public $table = 's';

    public function __construct()
    {
      parent::__construct();

    }

        public function add(array $ds = array())
        {
            if(!empty($ds))
            {
                $fields = "";
                $values = "";
                $i = 1;
                foreach($ds as $field => $value)
                {
                    $fields .= $i == 1? $field : ", ".$field;
                    $values .= $i == 1? "'".$value."'" : ", '".$value."'";
                    $i++;
                }

                $qr = "INSERT INTO ".$this->table." (".$fields.") VALUES (".$values.")";

                if($this->db->query($qr))
                {
                    return TRUE;
                }
                else
                {
                    $this->error = $this->db->error();
                    return FALSE;
                }
            }

            $this->error = 'Not found';
            return FALSE;
        }

我想插入循环100条以上记录的数据

但是我不知道如何使用codeigniter插入循环数据

谢谢

2 个答案:

答案 0 :(得分:0)

此HTML COCDE

 <div class="form-group">
      <label for="formClient-Cardnumber">Customer Code</label>
      <input type="text" class="form-control" name="code"  id="code" value="<?php echo $delimited; ?>"  readonly/>
    </div>
    <div class="form-group">
      <label for="formClient-Firstname">Amount</label>
      <input type="text" class="form-control" name="amount" id="amount"  placeholder="amount" autofocus />
    </div>

提交

我和ajax一起使用

<script>

  function createVoucher(){
    var code = $('#code').val();
    var amount = $('#amount').val();

    if(amount.length == 0){
      swal('amount');
      return false;
    }

    var data = $('#demo1').serialize();

    $.ajax({
      url:'save',
      type:'POST',
      cache:false,
      data: data,
      success:function(rs){
        var rs = $.trim(rs);
        if(rs == 'success')
        {
          swal({
            title:'Success',
            text:'ADD',
            type:'success',
            timer:1000
          });

          setTimeout(function(){
            window.location.href = 'add';
          }, 1500);

        }else{
          swal({
            title:'Error!',
            text:rs,
            type:'error'
          });
        }
      }
    });

    //$('#demo1').submit();
  }


$('#firstName').keyup(function(e){
  if(e.keyCode == 13){
    $('#lastName').focus();
  }
});

$('#lastName').keyup(function(e){
  if(e.keyCode == 13){
    $('#card_id').focus();
  }
});



</script>

答案 1 :(得分:0)

您不需要迭代插入,请使用insert_batch,我假设您的所有发布数据都是数组,所以:

public function save() {
    $data= [];
    foreach ($this->input->post('id_jenis_beras') as $key => $value) {
            $data[] = array(
                'code' => $this->input->post('code'),
                'amount' => $this->input->post('amount')[$key],
                'admin_id' => logged('id')
            );
        }
    }

    $rs = $this->db->insert_batch('your table name',$data);
    }
}