在 php 中的四列中显示 csv 数据
displaying csv data in four columns in php
我有 complaint.csv
文件,其中的数据如下:
1,complaint of health
2,complaint of money
.
.
.
71,complaint of bill
我想在 PHP 中的四列中显示以上数据,以便每列的行数相等。
我尝试了下面的 代码,但我无法获得成功。
<?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>
CSS
<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>
我收到 错误,例如 Notice: Uninitialized string offset
。受影响的行在 foreach loop
之间
谁能告诉我,上面的代码或任何其他解决方案出了什么问题?
这是一个解决方案:
- 列数可变
- 从左到右填充的行数不等
- 通过样式 (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 --------------------------------------------------------
?>
结果
希望对您有所帮助。通过使用 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;
?>
我有 complaint.csv
文件,其中的数据如下:
1,complaint of health
2,complaint of money
.
.
.
71,complaint of bill
我想在 PHP 中的四列中显示以上数据,以便每列的行数相等。
我尝试了下面的 代码,但我无法获得成功。
<?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>
CSS
<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>
我收到 错误,例如 Notice: Uninitialized string offset
。受影响的行在 foreach loop
谁能告诉我,上面的代码或任何其他解决方案出了什么问题?
这是一个解决方案:
- 列数可变
- 从左到右填充的行数不等
- 通过样式 (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 --------------------------------------------------------
?>
结果
希望对您有所帮助。通过使用 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;
?>