PHP在循环中断开并继续数组值

时间:2016-12-01 03:17:04

标签: php arrays excel codeigniter

是否有一种简单的方法可以双方分割(中断和继续)数组值(我的意思是两列)?

例如,我想获取如下表所示的数据:

   A           B                C               D                 E
CellRow |  Sampling Time  |  Sample Type |  Sampling Time  |  Sample Type
=========================================================================
  12          17:42             Start          11:42            Random
  13          17:42             Start          12:42            Random
  14          18:42             Random         12:42            Random
  15          18:42             Random         13:42            Random
  16          19:42             Random         13:42            Random
  17          19:42             Random         14:42            Random
  18          20:42             Random         14:42            Random
  19          20:42             Random         15:42            Random
  20          21:42             Random         15:42            Random
  21          21:42             Random         16:42            Random
  22          22:42             Random         16:42            Random
  23          22:42             Random         17:42            Random
  24          23:42             Random         17:42            Random
  25          23:42             Random         18:08            End
  26          08:42             Random         18:08            End
  27          08:42             Random
  28          10:42             Random
  29          10:42             Random
  30          11:42             Random

这是我将数据导出到excel文件的代码:

<?php

        $detail = $this->M_transactions->get_detail_byid($id);

        if(isset($detail)) {
            $totalDetail = count($detail); // for example totalDetail is 34, and above table is actually the real data
        } else {
            $totalDetail ='0';
        }

        // ====  setup display data  ====
        $row_percolumn = 19;   // MAX rows on table        

        if($totalDetail < $row_percolumn) {
            $worksheet = 1;
        } else {
            if(($totalDetail  % $row_percolumn) == 0) {
                $worksheet = $totalDetail / $row_percolumn;
            } else {
                $worksheet = floor(($totalDetail / $row_percolumn)) + 1;
            }
        }

        foreach($dtdetail as $dtdetail_row) {
            $sampling_time[]  = $dtdetail_row->sampling_time;
            $sample_type[]    = $dtdetail_row->sample_type;
        }

        for($i=0; $i < $worksheet; $i++) {
            $rowPage = 11; // start print detail after row 11 

            $start_detail = ($i * $row_percolumn);
            $finish_detail = (($i * $row_percolumn) + ($row_percolumn - 1));

            $data = $start_detail;

             while ($data <= $finish_detail) {
                $rowPage++;

                if(isset($sampling_time[$data])) { 
                  $dt_sampling_time[$data] = $sampling_time[$data]; 
                } else {
                  $dt_sampling_time[$data] = ''; 
                }

                if(isset($sample_type[$data])) { 
                  $dt_sample_type[$data] = $sample_type[$data]; 
                } else {
                  $dt_sample_type[$data] = ''; 
                }

                // print the data on left-side (cell B-C)
                $objPHPExcel->setCellValue('B'.$rowPage, $dt_sampling_time[$data]);
                $objPHPExcel->setCellValue('C'.$rowPage, $dt_sample_type[$data]);

                // break the loop and..
                if ($data == 19  && $rowPage == 30 ) {
                    // print the data on right-side (cell D-E)
                    $objPHPExcel->setCellValue('D'.($rowPage-19), $dt_sampling_time[$data]);
                    $objPHPExcel->setCellValue('E'.($rowPage-19), $dt_sample_type[$data]);
                }                


                $data++;                
             }            


        }


      ?>

我正在使用Codeigniter,我想将数据导出到Excel文件(我正在使用PHPExcel库),但我无法像上表一样拆分数据。 上面的代码不会在单元格DE上打印数据,而只会打印在单元格BC上。

任何帮助将不胜感激,谢谢。

0 个答案:

没有答案