CSV形式のファイルを読み込み、テーブル(表)で表示する方法。
サンプル
読み込むCSV形式ファイルの内容
読み込むファイル「sample.csv」の内容。
"1-1","1-2","1-3"
"2-1","2-2","2-3"
"3-1","3-2","3-3"
"2-1","2-2","2-3"
"3-1","3-2","3-3"
PHPコード
<?php
$file = "sample.csv";
if ( ( $handle = fopen ( $file, "r" ) ) !== FALSE ) {
echo "<table>\n";
while ( ( $data = fgetcsv ( $handle, 1000, ",", '"' ) ) !== FALSE ) {
echo "\t<tr>\n";
for ( $i = 0; $i < count( $data ); $i++ ) {
echo "\t\t<td>{$data[$i]}</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
fclose ( $handle );
}
?>
$file = "sample.csv";
if ( ( $handle = fopen ( $file, "r" ) ) !== FALSE ) {
echo "<table>\n";
while ( ( $data = fgetcsv ( $handle, 1000, ",", '"' ) ) !== FALSE ) {
echo "\t<tr>\n";
for ( $i = 0; $i < count( $data ); $i++ ) {
echo "\t\t<td>{$data[$i]}</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
fclose ( $handle );
}
?>
↓↓↓出力結果↓↓↓
出力結果
HTMLコード
<table>
<tr>
<td>1-1</td>
<td>1-2</td>
<td>1-3</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>2-3</td>
</tr>
<tr>
<td>3-1</td>
<td>3-2</td>
<td>3-3</td>
</tr>
</table>
<tr>
<td>1-1</td>
<td>1-2</td>
<td>1-3</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>2-3</td>
</tr>
<tr>
<td>3-1</td>
<td>3-2</td>
<td>3-3</td>
</tr>
</table>
ブラウザ表示例
1-1 | 1-2 | 1-3 |
2-1 | 2-2 | 2-3 |
3-1 | 3-2 | 3-3 |