I had to parse a csv file and read up on the fgetcsv function and was able to institute some code to parse a file and insert into a database table. My question was (for anyone experienced with using fgetcsv), is the following.
Here's an example of the CSV i have
#Line Number 1 has a # symbol with filed names
#Line Number 2 has a # symbol with file info
#Line Number 3 has a # symbol with date info
"Jan 1, 2006","Monday","45","Random";
"Jan 2,2006","Tuesday","50","Random";
etc etc etc
When I parse this file, It inputs the info on the fileds with # into my database. I have to manually edit each file before I parse it so its just the correct lines.
Is there any way using fgetcsv to ignore the lines that are either blank or have a # on them?
here's a sample of my fgetcsv code
$handle = fopen("./csv/test.csv", "r");
echo "Step 1 - opening test.csv<br><br>";
while loop below outputs the data in the array (number of fields in each row)
while (($data = fgetcsv($handle)) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for loop echos the data from fgetcsv fr each row where row is less then num which is the number of rows of data
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
$csvp = $dConn->Insert("bizstat", array(
"time"=>$data[0],
"t_bandwidth_mbs"=>$data[1],
"edge_response_bandwidth_mbs"=>$data[2],
"requests_bandwidth_mbs"=>$data[5],
"edge_traffic_hits_second"=>$data[6],
"not_found_errors_second"=>$data[8]));
echo $dConn->LastMethod . "</br>";
}
Thanks in advance for any insight.
Tony