loops - Traversing through CSV file in PHP -
i have code :
<?php $handle = fopen("generatepicklist.csv", "r"); $data = fgetcsv($handle, 5000, ","); ?>
which opens php file , converts php array. list out array contents , display them formatted page. have code , it's working.
<?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>"; ?>
what trying traverse through csv file , select data within coverage of $arraybegin
variable , last part of csv data. have done first line , working not know how increment pointer moves next line of csv file , same formula first line of data.
here's sample of csv content, 7 fields:
ÊÊ,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
i need select data each line , move on next line. how traverse next line using while loop? thank future responses.
this typically how go through csv file, starting have already:
$handle = fopen("generatepicklist.csv", "r"); $firstrow = fgetcsv($handle, 5000, ","); $partnumberposition = array_search("part number", $firstrow, true);
at point have read first line, contains field names. have determined @ column 'part number' at.
and fetch rest:
// function `fgetcsv()` return `false` when end-of-file reached while (false !== ($row = fgetcsv($handle, 5000))) { // have read a(nother) row of data // if you're not interested in columns before 'part number' // can slice them off $row = array_slice($row, $partnumberposition); // @ point, $row contains: // 0 => part number // 1 => quantity // etc... } // we're done, close file fclose($handle);
Comments
Post a Comment