!修订! 由于某种原因,它只打印一行csv,而将国家/地区打印两次?
<?php
$file = fopen('storelistdata.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
list($country[], $state[], $city[], $loc[]) = $line;
$arrayLength = count($line);
$i = 0;
while ($i < $arrayLength) {
echo "<a class=\"item\">" . $loc[$i] .",". $city[$i] .",". $state[$i] .",".$country[$i] . "<a/></br>";
$i++;
}
}
fclose($file);
?>
答案 0 :(得分:2)
编辑:
CSV文件
United States,Alabama,Alburn,Earth Fare
United States,Alabama,Huntsville,Earth Fare
United States,Alabama,Montgomery,Earth Fare
United States,Alabama,Hoover,Earth Fare
United States,Alabama,Fairhope,Fairhope Health Foods
代码:
<?php
$file = fopen('storelistdata.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
// Extract line array into these variables
list($country, $state, $city, $loc) = $line;
echo "<a class=\"item\">$loc, $city, $state, $country</a>\n";
}
fclose($file);
?>
结果:
<a class="item">Earth Fare, Alburn, Alabama, United States</a>
<a class="item">Earth Fare, Huntsville, Alabama, United States</a>
<a class="item">Earth Fare, Montgomery, Alabama, United States</a>
<a class="item">Earth Fare, Hoover, Alabama, United States</a>
<a class="item">Fairhope Health Foods, Fairhope, Alabama, United States</a>
答案 1 :(得分:0)
我认为您在此行上存在语法错误:
echo "<a class="item">" . $loc[$i] .",". $state[i] .",". $country[i] . "<a/>";
对于$state[i]
和$country[i]
,它们应分别为$state[$i]
和$country[$i]
。