2

我有这个代码:

<?php
   $handle = fopen("GeneratePicklist.csv", "r");
   $data = fgetcsv($handle, 5000, ",");
?>

这会打开一个 php 文件并将其转换为 php 数组。然后我将列出数组内容并将它们显示到格式化页面中。我有这个代码,它正在工作。

<?php
    echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
        <tr>
            <td class=\"dataHeader\" width=\"100\">Mat.Loc.</td>
            <td class=\"dataHeader\" width=\"20\">Qty</td>
            <td class=\"dataHeader\">Description</td>
            <td class=\"dataHeader\" width=\"150\">Part Number</td>
            <td class=\"dataHeader\" width=\"30\">Multiplier</td>
            <td class=\"dataHeader\" width=\"80\">Total Qty</td>
        </tr>
";
$locatePartNoString = array_search("Part Number",$data);
$arrayStart = $locatePartNoString-1;
end($data);
$lastField = key($data);
$counter = ($lastField - $arrayStart);
$arrayBegin = $locatePartNoString+6;
while($counter>0) {
    echo "
        <tr>
            <td class=\"centered\"><span class=\"title\">*".$data[$arrayBegin+4]."*</span><br>".$data[$arrayBegin+4]."</td>
            <td id=\"qty".$counter."\" class=\"centered\">".$data[$arrayBegin+1]."</td>
            <td>".$data[$arrayBegin+2]."</td>
            <td class=\"centered\">".$data[$arrayBegin]."</td>
            <td class=\"centered\"><input type=\"text\" id=\"multiplier".$counter."\" name=\"multiplier".$counter."\" value=\"1\" size=\"3\" style=\"border:1px dotted #CCC;\"></td>
            <td class=\"centered\"><input type=\"text\" id=\"total".$counter."\" name=\"total".$counter."\" value=\"1\" size=\"3\" style=\"border:1px dotted #CCC;\"></td>
        </tr>

    ";
    $counter--;
}
echo "</table>";
?>

我要做的是遍历 CSV 文件并仅在$arrayBegin变量覆盖范围内和我的 CSV 数据的最后部分选择某些数据。我已经完成了第一行并且它的工作很好,但我不知道如何递增,所以我的指针移动到 CSV 文件的下一行,并执行与我的第一行数据相同的公式。

这是 CSV 内容的示例,包含 7 个字段:

ÊÊ,Part Number,Qty,Description,BB Type,Material Location,Serial Number
ÊÊ,013224-001,1,"PCA,DDR2-800,MINIDIMM MOD256MBx 40",BOM,ASSY,ID121608ZS
ÊÊ,122657-00A,1,SOFTWARE TEST (US M3 CTO),BOM,ASSY,
ÊÊ,376383-002,8,"ASSY, BLANK,SFF",BOM,ASSY,
ÊÊ,458943-003,1,"CA ASSY, SFP BATTERY, 15 POS, 28AWG, 24",BOM,ASSY,
ÊÊ,460499-001,1,"ASSY, 4/V650HT BATTERY CHARGER MODULE",BOM,ASSY,
ÊÊ,499256-001,2,"ASSY, BLANK,MEDIA BAY,ML350G6",BOM,ASSY,
ÊÊ,500203-061,2,"DIMM,4GB PC3-10600R,256Mx4,RoHS",BOM,ASSY,RAKWF8DXV2Q100

我只需要从每一行中选择某些数据,然后继续下一行。如何使用我的 while 循环遍历到下一行?感谢您未来的回复。

4

2 回答 2

7

这通常是您浏览 CSV 文件的方式,从您已有的文件开始:

$handle = fopen("GeneratePicklist.csv", "r");
$firstRow = fgetcsv($handle, 5000, ",");
$partNumberPosition = array_search("Part Number", $firstRow, true);

此时您已经阅读了第一行,它通常包含字段名称。您还确定了“零件编号”在哪一列。

并获取其余部分:

// the function `fgetcsv()` will return `false` when the end-of-file is reached
while (false !== ($row = fgetcsv($handle, 5000))) {
    // we have read a(nother) row of data
    // if you're not interested in the columns before the 'Part Number'
    // you can slice them off
    $row = array_slice($row, $partNumberPosition);
    // at this point, $row contains:
    // 0 => the part number
    // 1 => the quantity
    // etc...
}
// we're done, close the file
fclose($handle);
于 2012-06-07T02:39:44.097 回答
2

在解析之后或在解析 while 循环期间,您可以测试条件。(这是我写的一个类方法,所以忽略类的东西:))

private function logicMakeCSVTable(){

    $this->importData = Array();
        $i = 0;
        while($row = fgetcsv($this->handle, 999999, ",")){

            $num = count($row);
            for($c=0; $c < $num; $c++){

                $this->importData[$i][$c] = $row[$c];
            }   

            $i++;
        }

        $_SESSION['ImportData']  = "";
        $_SESSION['ImportData'] = $this->importData;

}
于 2012-06-07T02:14:22.703 回答