将CSV中的数据插入数据库codeigniter

时间:2018-05-23 10:34:42

标签: php codeigniter

我需要您的建议,在CSV中将数据从Codeigniter插入到我的数据库中,之前我已成功,但在此情况下并非如此。所以在这种情况下,我需要先从另一个表中选择id并输入其值

这是我的控制器:

function imports($aid)
{
  $file_data = $this->csvimport->get_array($_FILES["csv_file"]["tmp_name"]);
  foreach(array_slice($file_data, 1) as $row)
  {
    $pid = $this->m_csvimport->getid($row["nip"]);
    $array [] =array(
      'aid' => $aid,
      'pid' => $pid,
      'kodeunik' => '',
      );
  }
  $this->m_csvimport->inserts($array);
}

这是我的模特:

function getid($nip)
{
    $this->db->select('id');
    $this->db->from('or_peserta');
    $this->db->where('nip',$nip);
    $query = $this->db->get();

    return $query->result();
}

function inserts($array)
{
    $this->db->insert_batch('or_tr_pa',$array);
}

如何根据Csv行获取ID,然后将其用于我的数组。请帮我修复我的错误。

3 个答案:

答案 0 :(得分:1)

您可以在foreach循环中使用一个计数器,如$ i = 1并将其插入到数组中,然后在foreach循环的末尾将其递增1

答案 1 :(得分:1)

希望这会对您有所帮助:

您应该只返回模型中的ID:

// The URL of the video file in our Bundle we want to play
NSURL *videoFileURL = [NSURL fileURLWithPath: videoFilePath];

playerItem =  [[AVPlayerItem alloc] initWithURL: videoFileURL];
player = [[AVPlayer alloc] initWithPlayerItem: playerItem];

// GPUCustomThreeInputFilter inherits from GPUImageThreeInputFilter
customThreeInputFilter = [[GPUCustomThreeInputFilter alloc] init];

GPUImageMovie *gpuImageMovie = [[GPUImageMovie alloc] initWithPlayerItem:playerItem];
[gpuImageMovie addTarget: customThreeInputFilter atTextureLocation:0];

// This texture will be available at "inputImageTexture2" in our fragment shader
[gpuImagePicture1 addTarget: customThreeInputFilter atTextureLocation:1];
[gpuImagePicture1 processImage];

// This texture will be available at "inputImageTexture3" in our fragment shader
[gpuImagePicture2 addTarget: customThreeInputFilter atTextureLocation:2];
[gpuImagePicture2 processImage];

// gpuImageView is an instance of GPUImageView and is added to our ViewController in the normal way via [self.view addSubview: gpuImageView]
[customThreeInputFilter addTarget: gpuImageView];

[gpuImageMovie startProcessing];
[player play];

您的控制器将保持原样

public function getid($nip)
{
    $this->db->select('id');
    $this->db->from('or_peserta');
    $this->db->where('nip',$nip);
    $query = $this->db->get();

    return $query->row()->id;
}

答案 2 :(得分:0)

     Please Try this one

   html file
        ---------------------------------------------------
        <form method="post" id="import_form" enctype="multipart/form-data">
                    <p><label>Select Excel File</label>
                    <input type="file" name="file" id="file" required ></p>
                    <br />
                    <input type="submit" name="import" value="Import" class="btn btn-info" />
        </form>

        Jquery
        --------------------------------------------------------------------

        $(document).ready(function(){

        $('#import_form').on('submit', function(event){
                event.preventDefault();
                        $.ajax({
                    url:"<?php echo base_url(); ?>index.php/Controller/import",
                    method:"POST",
                    data:new FormData(this),
                    contentType:false,
                    cache:false,
                    processData:false,
                    success:function(data){

                        $('#file').val('');
                         $('#omrs').html('<span style="color: green;">'+data+"</span>");

                    }
                })
            });

        });

        Controller
        --------------------------------------------------------------
    function import()

    {


        if(isset($_FILES["file"]["name"]))

        {

            $path = $_FILES["file"]["tmp_name"];

            $object = PHPExcel_IOFactory::load($path);

            foreach($object->getWorksheetIterator() as $worksheet)

            {

                    $highestRow = $worksheet->getHighestRow();

                    $highestColumn = $worksheet->getHighestColumn();

                    for($row=0; $row<=$highestRow; $row++)

                    {
                        $student_fk = $worksheet->getCellByColumnAndRow(0, $row)->getValue();

                    $student_subject_name = $worksheet->getCellByColumnAndRow(1, $row)->getValue();

    $data[] = array(

                            'student_fk'=>$student_fk,

        'student_subject_name'=>$student_subject_name




        );

            }

        }

        $this->Model->insertt($data);

        echo 'Data Imported successfully';

        }   

    }




        Model
        --------------------------------------------------------------

          public function insertt($data) 

           {

        $this->db->insert_batch('student_result', $data);


          return;

         }