PHP fgetcsv() 显示前 5 行
我正在将 CSV 文件导入到我的数据库中。我想显示前 5 行和列,这样我就可以了解文件中的内容。 因此,下面我编写了一个快速脚本来打开该文件。我计算列数,循环列数,为每个列创建,然后在下面抛出一个 while() 循环,以显示行。当然,我循环遍历要匹配的列数。
我成功地从文件中获取数据,甚至获得了正确的列数。但它显示的是文件中间某个位置的结果,而不是前 5 条记录。
第一行帮助我知道列的名称(如果有列名称)。
该脚本很脏,我很好奇如何优化它。
<?
$filerow = $numcols =0;
if(is_file($targetDirectoryFile)){
$file_opened = fopen($targetDirectoryFile, "r"); // open the file
?>
<table class="itemstable" style="width:100%;">
<?
$getfileInfo = fgetcsv($file_opened);
$numcols = count($getfileInfo); // count columns
?>
<tr>
<? for($cols = 0; $cols < $numcols; $cols++ ){ // loop thru # of columns ?>
<th>Column <?=$cols+1;?></th>
<? } ?>
</tr>
<?
// feels like this could be done better.
while ($getfileInfo2 = fgetcsv($file_opened)){
$filerow++;
?>
<tr>
<? for($col = 0; $col < $numcols; $col++ ){ // loop thru # of columns ?>
<td><?=trim($getfileInfo2[$col]);?></td>
<? } ?>
</tr>
<?
if($filerow > 4){ break; fclose($file_opened); // stop and close after 5 loops }
}
echo '</table>';
//fclose($file_opened);
}
?>
I'm importing CSV files into my database. I want to display the first 5 rows with columns, so I can get an idea of what's inside the file.
So below I crafted a quick script to open the file. I count the number of columns, loop through the number of columns, create for each, and then I throw a while() loop below that, to display the rows. Of course, I loop through the number of columns to match.
I'm successfully getting data from the file, and I'm even getting the correct number of columns. But it's displaying result from somewhere in the middle of the file, rather than the first 5 records.
The first row helps me know what the columns are called, (if there are column names).
The script is dirty, and I'm curious how it can be optimized.
<?
$filerow = $numcols =0;
if(is_file($targetDirectoryFile)){
$file_opened = fopen($targetDirectoryFile, "r"); // open the file
?>
<table class="itemstable" style="width:100%;">
<?
$getfileInfo = fgetcsv($file_opened);
$numcols = count($getfileInfo); // count columns
?>
<tr>
<? for($cols = 0; $cols < $numcols; $cols++ ){ // loop thru # of columns ?>
<th>Column <?=$cols+1;?></th>
<? } ?>
</tr>
<?
// feels like this could be done better.
while ($getfileInfo2 = fgetcsv($file_opened)){
$filerow++;
?>
<tr>
<? for($col = 0; $col < $numcols; $col++ ){ // loop thru # of columns ?>
<td><?=trim($getfileInfo2[$col]);?></td>
<? } ?>
</tr>
<?
if($filerow > 4){ break; fclose($file_opened); // stop and close after 5 loops }
}
echo '</table>';
//fclose($file_opened);
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于优化,您可以通过使用以下方式摆脱循环中的
if
语句:我也不认为您的文件会正确关闭,因为您将语句放在
break
之后code> 语句,但我不知道这会如何导致您遇到的问题。关于中间的输出;输入文件和生成的 html 是什么样子的?
As to optimization, you can get rid of the
if
statement in your loop by just using:I also don't think your file gets closed correctly as you put the statement after the
break
statement, but I don't see how that could lead to the problem you are having.About the output from the middle; what do the input file and the generated html look like?