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

时间:2015-04-10 08:09:30

标签: php html mysql css csv

我是新手使用csv文件。我想使用CSV文件,以便更新我的表的数据将更容易。

EXPECTED TABLE应该是这样的: https://www.dropbox.com/s/e6sg9d3fsh8n1v9/table.png?dl=0

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

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文件,如下所示:

<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

RESULT NOT 与上面的EXPECTED TABLE类似。我试图通过更改PHP代码获取表,但无法在表的正确位置获取数据。我无法理解.csv文件数据的安排或在PHP中读取该文件会出现什么问题。还有另一个问题,如在EXPECTED TABLE中,行css是不同的。因此,我不能使用单<tr><td>来获取数据。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

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

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

<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 :(得分:0)

<?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>