首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PHP中使用CSV文件更新表的内容

在PHP中使用CSV文件更新表的内容
EN

Stack Overflow用户
提问于 2015-04-10 08:09:30
回答 2查看 210关注 0票数 2

我是第一次使用csv文件。我想使用CSV文件,以便更容易地更新我的表的数据。

预期的表应该如下所示:https://www.dropbox.com/s/e6sg9d3fsh8n1v9/table.png?dl=0

我为上表生成了.csv文件,如下所示:

代码语言:javascript
复制
Americas Center,XX%,xxxx,xxxxx,Irving,Norcross,San Carlos
Asheville,XX%,xxxx,xxxxx,Louisville,Northfield,San Diego
Austin,XX%,xxxx,xxxxx,Manchester,Overland Park,Telecommuters
Buffalo Grove,XX%,xxxx,xxxxx,Marlborough,Pennsylvania,Tempe
Charlotte,etc,,,Memphis,Plano,Valencia
Columbia,,,,Milpitas,Raleigh,
Coopersville,,,,Morgan Hill,RTS,

我在PHP中读取了.csv文件,如下所示:

代码语言:javascript
复制
<table class="local table-fill">
<?php

$file_handle = fopen("flextronics.csv", "r");

while (!feof($file_handle) ) {

$line_of_text = fgetcsv($file_handle, 1024);

?>
<tr>
<td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[0]; ?></td>
</tr>
<tr>
<td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[1]; ?></td>
</tr>
<tr>
<td class="text-left" style="color:#FFF;background:#9ea7af;"><strong>Eligible:</strong> <?php print $line_of_text[2]; ?></td>
</tr>
<tr>
<td class="text-left" style="color:#FFF;background:#9ea7af;"><strong>Registered:</strong> <?php print $line_of_text[3]; ?></td>
</tr>
<tr>
<td class="text-left"><?php print $line_of_text[4]; ?></td>
</tr>
<tr>
<td class="text-left"><?php print $line_of_text[5]; ?></td>
</tr>
<tr>
<td class="text-left"><?php print $line_of_text[6]; ?></td>
</tr>
<?php   
}
fclose($file_handle);
?>   
</table>

我得到了这样的结果:https://www.dropbox.com/s/fjqoj1400nqkgnx/result.png?dl=0

结果是,而不是像上面预期的表那样的。我试图通过更改PHP代码获取表,但无法在表的正确位置获取数据。我无法理解.csv文件数据的排列或在PHP中读取该文件的错误。还有另一个问题,如在预期表中,行css是不同的。因此,考虑到这一点,我不能使用单个<tr><td>来获取数据。我该怎么做?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-10 08:32:53

代码语言:javascript
复制
<?php

$file_handle = fopen("flextronics.csv", "r");

while (!feof($file_handle)) {

$lines_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
?>

<table class="local table-fill" >
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[0]; ?></td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[1]; ?></td>
<? endforeach; ?>
</tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left" style="color:#FFF;background:#9ea7af;">
        <strong>Eligible:</strong> <?php print $line_of_text[2]; ?></td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left" style="color:#FFF;background:#9ea7af;">
        <strong>Registered:</strong> <?php print $line_of_text[3]; ?></td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left">
           <?php if ( ! empty($line_of_text[4]) ) print $line_of_text[4]; ?>
    </td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left">
        <?php if ( ! empty($line_of_text[5]) ) print $line_of_text[5]; ?>
    </td>
<? endforeach; ?>
</tr>
<tr>
<?php foreach ( $lines_of_text as $line_of_text): ?>
    <td class="text-left">
       <?php if ( ! empty($line_of_text[6]) ) print $line_of_text[6]; ?>
    </td>
<? endforeach; ?>
</tr>
</table>
票数 1
EN

Stack Overflow用户

发布于 2015-04-10 08:17:51

在您的代码中,您将为$line_of_text的每一列创建一个新行。

尝试使用下面提到的代码:

代码语言:javascript
复制
<table class="local table-fill" >
<?php

$file_handle = fopen("flextronics.csv", "r");

while (!feof($file_handle)) {

$line_of_text = fgetcsv($file_handle, 1024);

?>
<tr>
    <td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[0]; ?></td>

    <td class="text-left" style="color:#FFF;background:#9ea7af;"><?php print $line_of_text[1]; ?></td>
    <td class="text-left" style="color:#FFF;background:#9ea7af;">
        <strong>Eligible:</strong> <?php print $line_of_text[2]; ?></td>
    <td class="text-left" style="color:#FFF;background:#9ea7af;">
        <strong>Registered:</strong> <?php print $line_of_text[3]; ?></td>
    <td class="text-left"><?php print $line_of_text[4]; ?></td>
    <td class="text-left"><?php print $line_of_text[5]; ?></td>
    <td class="text-left"><?php print $line_of_text[6]; ?></td>
</tr>
<?php
}
fclose($file_handle);
?>
</table>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29556412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档