JudeJitsu
JudeJitsu

Reputation: 748

Traversing through CSV file in PHP

I have this code :

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

Which opens up a php file and converts it into a php array. Then I will list out the array contents and display them into a formatted page. I have this code and 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 I am trying to do is the traverse through the CSV file and select certain data only within the coverage of $arrayBegin variable and the last part of my CSV data. I have done the first line and its working good but I do not know how to increment so my pointer moves to the next line of the CSV file and do the same formula as my first line of data.

Here's a sample of the 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 just need to select certain data from each line and then move on to the next line. How do i traverse to the next line using my while loop? Thank you for your future responses.

Upvotes: 2

Views: 8542

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173522

This is typically how you go through a CSV file, starting with what you have already:

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

At this point you have read the first line, which normally contains the field names. You have also determined at which column the 'Part Number' is at.

And to fetch the rest:

// 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);

Upvotes: 7

Alex Kremer
Alex Kremer

Reputation: 128

After you parse or during the parse of the while loop you can test for conditions. (This is a class method I wrote so ignore the class stuff :))

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;

}

Upvotes: 2

Related Questions