[英]How to read a range of rows from CSV file to JSON array using PHP to handle large CSV file?
[英]Extract a range of rows from CSV file using PHP
我是PHP的新手,一直在尝试与CSS结合将CSV文件转换为我网站上的表格。 我想知道是否可以选择仅显示CSV文件中的某一行范围,例如5到20行,然后发现下面的代码可以显示某些列。
有没有一种简单的方法可以将其切换为显示选定的行?
<?php
$row = 1;
if (($handle = fopen("donors.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<td style="border-top: 1px solid rgb(111,180,224); border-left: 1px solid rgb(111,180,224); border-bottom: 1px solid rgb(111,180,224);" align="left" bgcolor="#0066cc" height="36" valign="middle" ><b><font color="#ffffff" size="2"> '.$value.' </font></b></td>';
}else{
echo '<td style=" border-bottom: 1px solid rgb(111,180,224);" sdval="9" sdnum="1040;" align="left" bgcolor="#ffffff" height="25" valign="middle"><font color="#000000" size="2"> '.$value.' </font></td>';
}
}
if ($row == 1) {
echo '</tr>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
echo '</center>';
fclose($handle);
}
?>
选择行范围:
我从一个旧项目改编了此代码,但它应该可以工作。
<?php
echo "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"/>
<link rel=\"stylesheet\" type=\"text/css\" href=\"test.css\">";
$file = '/volume1/web/Print data.csv';
$lines = file($file);
if (file_exists($file)){
//echo "$file exists<br/>";
echo "<table>"; //create the html table
foreach ($lines as $line) { //loop to fill your table with csv data
$expl = explode(",", $line); //explode each line on "," to create the rows
echo "<tr>"; //create each line of your table
$c=5;
while ($c<=20) { //loop to extract rows 5 to 20
if(($c % 2) == 1){ //odd rows
echo "<td class=\"odd\">".$expl[$c]."</td>";
}
else{
echo "<td class=\"even\">".$expl[$c]."</td>"; //even rows
}
$c++;
}
echo "</tr>";
}
echo "</table>";
}
else{
echo "file is not here: $file";
}
?>
对于CSS:
您应该使用带有此代码的外部.css
文件
.odd {
border-top: 1px solid rgb(111, 180, 224);
border-left: 1px solid rgb(111, 180, 224);
border-bottom: 1px solid rgb(111, 180, 224);
background:#0066cc;
color:white;
font-weight:bold;
font-size:12px;
}
.even {
border-bottom: 1px solid rgb(111, 180, 224);
background: #ffffff;
color:#000000;
font-size:12px;
}
要检查此CSS,您可以看到此jsfiddle
希望对您有帮助。
编辑
修正了一些缺失;
。 添加了结果图片
我不太确定您想显示什么。
但是在测试网站上,我添加了一张表格,显示了多行。
这是此表的代码:
echo "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"/>
<link rel=\"stylesheet\" type=\"text/css\" href=\"test.css\">";
$file = '/var/www/test/test.csv';
echo "<h1><b>Lines X to Y</b></h1>";
echo "<table>"; //create the html table
$x=2; //define the first line to display
$y=4; //define the last line to display
$line_counter=0;
foreach ($lines as $line) { //loop to fill your table with csv data
if($x<=$line_counter && $line_counter<=$y)
{
$expl = explode(",", $line); //explode each line on "," to create the rows
echo "<tr>"; //create each line of your table
$c=0;
while ($c<=26) { //loop to extract rows 5 to 20
if(($c % 2) == 1){ //odd rows
echo "<td class=\"odd\">".$expl[$c]."</td>";
}
elseif(($c == 0)){
echo "<td class=\"even\">".$expl[$c]."</td>";
}
else{
echo "<td class=\"even\">".$expl[$c]."</td>"; //even rows
}
$c++;
}
echo "</tr>";
$x++;
}
$line_counter++;
}
echo "</table>";
echo "<h1><b>content of the file</b></h1>";
foreach ($lines as $line) { //loop to fill your table with csv data
echo "$line<br/>" ;
}
CSS始终位于单独的文件中,与我的第一个答案相同。
1)使用以下函数将CSV文件提取到数组中(来源: jaywilliams @ GIST )
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
2)迭代所需的行范围
$data = csv_to_array($file,$delimiter);
echo "your table header stuff"; //<table> ...
//first row numbered as 1
$from_row = 2; //second
$to_row = 50;
//first row numbered as 0
for($i = from_row-1; $i< $to_row-1; $i++)
{
echo "<tr>"; //start row
//now, every row of the CSV file is an array and consists of fileds=>values
//so now you are dealing with columns
foreach($data[$i] as $key=>$value
{
echo "<td>" . $value . "</td>";
}
echo "</tr>"; //end row
}
echo "your table footer stuff"; //</table> ...
从这里开始,我想您可以使用CSS设置表格样式,从而随心所欲。 重要的是,如果要使用表头,则可以向函数提供一个CSV文件,其第一行中保存有列名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.