在php中的四列中显示csv数据

时间:2016-02-01 10:43:11

标签: php csv fgetcsv

我有req个文件,其中的数据如下所示:

ctrl.func1 = function(req, res, next){
     // Save to db
     SomeSchemaModel(req.body.data).save(function(err, doc){
          if(!err){
               // assuming that theres no error
               // how can I pass the doc._id
               // to the next middleware
               // using the next() fucntion?
               req.docId = doc._id;
               next();
          }     
     }

}


ctrl.func2 = function(req, res){
     // do something with the _id
     // from the previous func1
     console.log(req.docId);
}

我想在PHP的四列中显示上面的数据,这样每列的行数都相等。

我在代码下面尝试过,但我无法取得成功。

complaint.csv

CSS

1,complaint of health
2,complaint of money
.
.
.
71,complaint of bill

我收到{strong>错误,如 <?php $fp = file('../complaint.csv', FILE_SKIP_EMPTY_LINES); $total_rows = count($fp); $count = $total_rows; $first_col = ceil($count/ 4); $count -= $first_col; $second_col = ceil($count/ 3); $count -= $second_col ; $third_col = ceil($count/ 2); $forth_col = $count - $third_col ; while (!feof($fp)) { $lines[] = fgetcsv($fp, 1024); } fclose($fp); ?> <div class="added"> <div class="column-left"> <?php for ($i = 0; $i < $first_col; $i++) { foreach ( $lines as $line): ?> <label class="checkbox" for="<?php print 'checkbox'.$line[$i][0]; ?>" style="font-size:20px;"> <input type="checkbox" name="complaint" value="<?php print $line[$i][0]; ?>" id="<?php print 'checkbox'.$line[$i][0]; ?>" data-toggle="checkbox"> <?php print $line[$i][1]; ?> </label> <?php endforeach; } ?> </div> <div class="column-center"> <?php $k = $i; for ($j = 0; $j < $second_col; $j++) { foreach ( $lines as $line): ?> <label class="checkbox" for="<?php print 'checkbox'.$line[$j][0]; ?>" style="font-size:20px;"> <input type="checkbox" name="complaint" value="<?php print $line[$j][0]; ?>" id="<?php print 'checkbox'.$line[$j][0]; ?>" data-toggle="checkbox"> <?php print $line[$j][1]; ?> </label> <?php endforeach; $k++; } ?> </div> <div class="column-center-right"> <?php $m = $k; for ($l = 0; $l < $third_col; $l++) { foreach ( $lines as $line): ?> <label class="checkbox" for="<?php print 'checkbox'.$line[$l][0]; ?>" style="font-size:20px;"> <input type="checkbox" name="complaint" value="<?php print $line[$l][0]; ?>" id="<?php print 'checkbox'.$line[$l][0]; ?>" data-toggle="checkbox"> <?php print $line[$l][1]; ?> </label> <?php endforeach; $m++; } ?> </div> <div class="column-right"> <?php $n = $k; for ($p = 0; $p < $forth_col; $p++) { foreach ( $lines as $line): ?> <label class="checkbox" for="<?php print 'checkbox'.$line[$p][0]; ?>" style="font-size:20px;"> <input type="checkbox" name="complaint" value="<?php print $line[$p][0]; ?>" id="<?php print 'checkbox'.$line[$p][0]; ?>" data-toggle="checkbox"> <?php print $line[$p][1]; ?> </label> <?php endforeach; $n++; } ?> <br/> </div> </div> 。受影响的行位于 <style> .column-left{ float: left; width: 25%; } .column-right{ float: right; width:25%; } .column-center{ float: left; width: 25%; } .column-center-right{ float: left; width: 25%; } div.added { padding: 0px 0px 5px 0px; font-size: 22px; font-family: "freightbook"; color: #2a4753; text-align: left; } </style>

之间

任何人都可以告诉我,上面的代码或其他任何解决方案出了什么问题?

2 个答案:

答案 0 :(得分:0)

这是一个解决方案:

  • 可变列数
  • 从左到右填充不等数量的行
  • 通过样式(CSS)格式化HTML元素

输入(complaint.csv)

1,complaint of health
2,complaint of money
3,complaint type 3
4,complaint type 4
5,complaint type 5
6,complaint type 6
7,complaint type 7
8,complaint type 8
9,complaint of bill

代码

<?php

    // Setup ---------------------------------------------------------------        
    define('numcols',4);  // set the number of columns here
    $csv = array_map('str_getcsv', file('./complaint.csv'));
    $numcsv = count($csv);
    $linespercol = floor($numcsv / numcols);
    $remainder   = ($numcsv % numcols);
    // Setup ---------------------------------------------------------------        

    // Page, part 1 --------------------------------------------------------
    echo '<html
    <head>
    <style type="text/css">
        BODY { font-family:tahoma,arial,helvetica,sans-serif; font-size:76%; }
        .table { background-color: #e0e0e0; }
        .break { break:both; border:0; with:1px; }
        .column {  border:0; float:left; padding:0.333em; }
    </style>
    </head>
    <body>
    ';    
    // Page, part 1 --------------------------------------------------------

    // The n-column table --------------------------------------------------
    echo '<div class="table">'.PHP_EOL;
    echo '   <div class="column">'.PHP_EOL;

    $lines = 0;
    $lpc   = $linespercol;
    if ($remainder>0) { $lpc++; $remainder--; }
    foreach($csv as $item) {
        $lines++;
        if ($lines>$lpc) {
            echo '   </div>' . PHP_EOL . '<div class="column">'.PHP_EOL;
            $lines = 1;
            $lpc   = $linespercol;
            if ($remainder>0) { $lpc++; $remainder--; }
        }
        echo '      <label class="checkbox" for="checkbox'.$item[0].'" style="font-size:20px;">
                    <input type="checkbox" name="complaint" value="'.$item[0].'" id="'.$item[0].'" data-toggle="checkbox">'
                        .$item[1].
                    '</label><br />';
    }
    echo '   </div>'.PHP_EOL;
    echo '</div>'.PHP_EOL;
    // The n-column table --------------------------------------------------

    // Page, part 2 --------------------------------------------------------             
    echo '</body>
    </html>
    ';
    // Page, part 2 --------------------------------------------------------        

?>

结果

enter image description here

答案 1 :(得分:0)

希望这会对你有所帮助。通过使用PHP内置的SPL类。

 <?php
            $file = new SplFileObject("./FL_insurance_sample.csv", "r");
            $file->setFlags(SplFileObject::READ_CSV);
            $file->seek($file->getSize());
            $linesTotal = $file->key();
            $recordsInColumn = floor($linesTotal / 4);
            $res = array();
            foreach ($file as $line) :
                $res[] = $line;
            endforeach;
            $finalRecord = array_chunk($res, $recordsInColumn);
            $classNames = array('column-left','column-right','column-center','column-center-right');
            foreach ($finalRecord as $key => $data) : ?>
            <div class="<?php echo $classNames[$key]; ?>"> 
                <?php foreach ($data as $key1 => $row) :
                    if ($row[0] !== null && $row[1] !== null):
                        ?>
                        <label class="checkbox" for="<?php print 'checkbox' . $row[0]; ?>" style="font-size:20px;">
                            <input type="checkbox" name="complaint" value="<?php print $row[0]; ?>" id="<?php print 'checkbox' . $row[0]; ?>" data-toggle="checkbox">
                            <?php print $row[1]; ?>
                        </label>
                        <?php
                    endif;
                endforeach; ?>
             </div>   
            <?php endforeach;
    ?>