简体   繁体   English

PHP自定义CSV读取,不同列和行的独立回显

[英]PHP custom CSV read, independent echos for different columns and rows

I have a simple CSV with let's say 6 columns. 我有一个简单的CSV,其中有6列。

EDIT: I can't find the mistake in the following, it's to do with selecting a key and using it as a variable I think: 编辑:在下面我找不到错误,这与选择一个键并将其用作我认为的变量有关:

$handle = fopen('hire_pa_amps.csv',"r");
while($values = fgetcsv($handle))
{
foreach('$values[0]' as $value0)
{
    echo($value0);
}
foreach('$values[1]' as $valuepound)
{
    echo('&pound'.$valuepound);
}
echo ('<br>'); }
fclose($handle); 



The data from the CSV file looks like this:
 "H/H ELECTRONICS VX200, 100 watts/channel stereo amplifier",70,30,20
  Electrovoice CP1800,84,36,24

In the meantime I've managed to do it somehow differently (and it works, for now) but I still want to know where the problem is in the code above (how to select a key in a array and manipulate it independently while using foreach). 在此期间,我设法以某种方式进行了处理(暂时可以使用),但我仍然想知道上面代码中的问题所在(如何在使用foreach的同时选择数组中的键并独立进行操作) )。 So the solution that I made for now: 所以我现在做的解决方案是:

$handle = fopen('hire_pa_amps.csv','r') or die("can't open file");

echo('<table class="pricesTable">');
while($csv_line = fgetcsv($handle)) {
    list($column1, $column2, $column3, $column4) = $csv_line;

    echo('<tr>');

    echo    '<td>'.$column1.'</td>'.
            '<td>'.'&pound'.$column2.'</td>'.
            '<td>'.'&pound'.$column3.'</td>'.
            '<td>'.'&pound'.$column4.'</td>';
    echo('</tr>');
    }
fclose($handle) or die("can't close file");
echo('</table>');

If you change your CSV to include column headers and then edit the column selection in the code below then this will work: 如果您将CSV更改为包括列标题,然后在下面的代码中编辑列选择,则此方法将起作用:

function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
    return FALSE;

$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
    while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
    {
        if(!$header)
            $header = $row;
        else
            $data[] = array_combine($header, $row);
    }
    fclose($handle);
}
return $data;
}

$csvArray = csv_to_array('hire_pa_amps.csv');
echo "<table>
<tr>
    <th>Product Name</th>
    <th>Price</th>
    <th>Markup</th>
    <th>VAT</th>
</tr>
";
foreach($csvArray as $currentProduct){
    echo "  <tr>
    <td>" . $currentProduct['Product Name'] . "</td>
    <td>" . $currentProduct['Price'] . "</td>
    <td>" . $currentProduct['Markup'] . "</td>
    <td>" . $currentProduct['VAT'] . "</td>
</tr>
";
}
echo "</table>";

I don't think you were thinking about the way the CSV was parsed to an array properly with your foreach() 's, you were trying to loop on $values[0] when you should have been looping on $values. 我认为您没有考虑过使用foreach()将CSV正确解析为数组的方式,而是试图在$ values上循环时尝试在$ values [0]上循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM