我有这段代码:
<?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循环遍历下一行?感谢您将来的回复。
答案 0 :(得分:7)
这通常是您浏览CSV文件的方式,从您已经拥有的文件开始:
$handle = fopen("GeneratePicklist.csv", "r");
$firstRow = fgetcsv($handle, 5000, ",");
$partNumberPosition = array_search("Part Number", $firstRow, true);
此时您已阅读第一行,该行通常包含字段名称。您还确定了&#39;部件编号&#39;是在。
要取其余部分:
// 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);
答案 1 :(得分: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;
}